The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

401 error on list creation

Highlighted
Occasional Participant

401 error on list creation

I'm using the OAuth method to access a ConstantContact user account.  I can view contacts and lists, but when I try to create a new list via the REST API call it returns a 401 error.  I'm using code from the ctctWrapper.php file to build the XML file.  Is there any info I can use to see what I'm doing wrong?   


Here's some code to show you what I'm doing and if there is something I'm doing wrong.



            //Auth request with stored OAuth Creds


            $list_api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl.'lists');


            $list_api_request->sign_request($sig_method, $oauth_consumer, $authorization_token);


       $list_api_request->get_normalized_http_method();


 


            //Send POST with XML to the lists REST API url


            $list_request = curl_init($webServiceUrl.'lists');


curl_setopt($list_request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");


curl_setopt($list_request, CURLOPT_HEADER, 0);


curl_setopt($list_request, CURLOPT_RETURNTRANSFER, 1);


curl_setopt($list_request, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", "accept:application/atom+xml", 'Content-Length: ' . strlen($listXML)));


   curl_setopt($list_request, CURLOPT_FAILONERROR, 1);


curl_setopt($list_request, CURLOPT_SSL_VERIFYPEER, 0);


curl_setopt($list_request, CURLOPT_CUSTOMREQUEST, 'POST');


curl_setopt($list_request, CURLOPT_POSTFIELDS, $listXML);



   //Execute cURL request and get return.


$aero79_new_list = curl_exec($list_request);


$aero79_new_list = curl_getinfo($list_request);


$aero79_new_list = curl_error($list_request);


                   curl_close($list_request);


1 REPLY 1
Highlighted
Moderator

Problem seems to be around an

Problem seems to be around an error in creating the OAuth Authenticated URL.  If you see this post in how to Authenticate using OAuth and PHP, you'll see that the OAuth request must be converted to a request URL that Curl can consume, otherwise this will fail.  The change is actually two lines, see lines 7-8 below. 





  1. //Auth request with stored OAuth Creds



  2. $list_api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl.'lists');



  3. $list_api_request->sign_request($sig_method, $oauth_consumer, $authorization_token);



  4. $list_api_request->get_normalized_http_method();



  5.  



  6. //Send POST with XML to the lists REST API url



  7. $request_url  = $list_api_request->to_url();



  8. $session = curl_init($request_url);



  9. curl_setopt($list_request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");



  10. curl_setopt($list_request, CURLOPT_HEADER, 0);



  11. curl_setopt($list_request, CURLOPT_RETURNTRANSFER, 1);



  12. curl_setopt($list_request, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", "accept:application/atom+xml", 'Content-Length: ' . strlen($listXML)));



  13. curl_setopt($list_request, CURLOPT_FAILONERROR, 1);



  14. curl_setopt($list_request, CURLOPT_SSL_VERIFYPEER, 0);



  15. curl_setopt($list_request, CURLOPT_CUSTOMREQUEST, 'POST');



  16. curl_setopt($list_request, CURLOPT_POSTFIELDS, $listXML);



  17.  



  18. //Execute cURL request and get return.



  19. $aero79_new_list = curl_exec($list_request);



  20. $aero79_new_list = curl_getinfo($list_request);



  21. $aero79_new_list = curl_error($list_request);



  22. curl_close($list_request);



  23.  



  24.  



Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured