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.

Getting Http 500

Highlighted
Occasional Participant

Getting Http 500

Hello,

I need to access few web services API's but i am getting 500 when accessing api with oauth. I am getting the access_token and oauth_token_secret . But accessing api i am gettong 500. For example when i try to access $api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl);

The $webServiceUrl URL is 'https://api.constantcontact.com/ws/customers/'.$username.'/'. Any help wil be appreciated.


Below is my PHP code. Please help if i am making any mistake :-



define("URI", "https://oauth.constantcontact.com");

$request_token_url = URI.'/ws/oauth/request_token';

$parsed = parse_url($request_token_url);

//$params = array('oauth_callback' => 'http://lnkts.com/linksms/v2/signin.php' , 'oauth_consumer_key' => "c1a7760a-3942-4c36-b8e9-29ab6f4b8121" , 'oauth_signature_method' => 'HMAC-SHA1');

$cContactKey = "My KEY";

$cContactSecret = "My SECRET";

$username = "pvnarula";//My USERNAME



$access_url = URI.'/ws/oauth/access_token';

//$webServiceUrl = 'https://api.constantcontact.com/ws/customers/'.$username.'/contacts';

$webServiceUrl = 'https://api.constantcontact.com/ws/customers/'.$username.'/';

$callback_url = 'http://lnkts.com/linksms/v2/signin.php';



if(!$_GET){



    // Create new OAuth Consumer

    $oauth_consumer = new OAuthConsumer($cContactKey, $cContactSecret);

    $params = array('oauth_callback' => $callback_url);

  

    // Create an OAuth Request to obtain request token

    $tokenRequest = OAuthRequest::from_consumer_and_token($oauth_consumer, NULL, 'GET', $request_token_url, $params);

  

    // Sign the request using HMAC_SHA1 signature method

    $tokenRequest->sign_request($sig_method, $oauth_consumer, NULL);

  

    // Convert the request parameters to a URL

    $token_request_url  = $tokenRequest->to_url();

  

    // Excecute request for a request token / secret

   

    $session = curl_init($token_request_url);

    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);

    $response = curl_exec($session);

    if (!$response) {

        $response = curl_error($session);

        echo $response;

    }

    curl_close($session);

    // Retrieve the returned request token and request secret

    parse_str($response, $params);

    $oauth_token = $params;

    $oauth_token_secret = $params;

   

    //$_SESSION = $oauth_token;

       //$_SESSION = $oauth_token_secret;

   

    $info = $oauth_token."[]".$oauth_token_secret;

    setcookie('requestToken', $info, time()+120, '/');

    /*

     * Create the authentication URL. This is where the user is prompted

     * to authorize this application to access resources in their Constant Contact account.

     */

  

    $ccontact_auth_url = URI.'/ws/oauth/confirm_access?oauth_token='.$oauth_token.'&oauth_callback='.urlencode($callback_url);

   

  

} else {

  

    // At this point we assume the callback_url has been used, and the oauth_verifier has been set

    $oauth_verifier = $_GET;

  

    // Retrieve the request token and secret from the requestToken cookie

    $requestToken = explode('[]', $_COOKIE);

    $request_token = $requestToken;

    $request_token_secret = $requestToken;

    //$request_token = $_SESSION;

    //$request_token_secret = $_SESSION;

      

    // Create OAuth request for an access token

    $oauth_consumer = new OAuthConsumer($cContactKey, $cContactSecret);

    $access_token = new OAuthToken($request_token, $request_token_secret);

    $access_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $access_token, 'GET', $access_url);

  

    //Set verifier returned with the request token

    $access_request->set_parameter('oauth_verifier', $oauth_verifier);

  

    // Sign the request using HMAC_SHA1 signature method

    $access_request->sign_request($sig_method, $oauth_consumer, $access_token);

  

    // Execute access token request

    $request  = $access_request->to_url();

    $session = curl_init($request);

    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);

    $response = curl_exec($session);

    if (!$response) {

        $response = curl_error($curl);

        echo $response; 

    }

    curl_close($session);

   

    // Retrieve the access_token and access_token_secret 

    parse_str($response, $params);

    $access_token = $params;

    $access_token_secret = $params;

  

    /*

     * The user has now granted this application access to its resources, and we

     * can begin to make http requests for the authenticated user.

     */

  

    //Create API Request

    $oauth_consumer = new OAuthConsumer($consumer_key, $consumer_secret);

    $authorization_token = new OAuthToken($access_token, $access_token_secret);

    $api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl);



    // Sign request

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

    $api_request->get_normalized_http_method();

  

    // Execute HTTP Request

    $request_url  = $api_request->to_url();

    $session = curl_init($request_url);

    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);

    $response = curl_exec($session);

    if (!$response) {

        $response = curl_error($curl);

        echo $response; 

    }

    $error = curl_error($session);

    curl_close($session);

      

    print_r($response);die;

}


Regards

Priyavrat

Priyavrat

2 REPLIES 2
Highlighted
Occasional Participant

Solved

Thanks, I solved it by myself.

Priyavrat

Highlighted
Employee

Re: Getting Http 500

Hi Priyavrat,


There are 3 problems that I'm seeing with your OAuth example which seem to be causing issues, some of which may just be in what you had pasted and not in the actual code you're running:


1. The OAuth.php library is not included, but I am assuming this is just in what is pasted here. You would just need to add:


require_once('OAuth.php');

to the top or file, assuming that is the location of the OAuth Library.


2. I don't see the following code anywhere:


$sig_method = new OAuthSignatureMethod_HMAC_SHA1();

This is also something I'm assuming is just in what you have pasted, otherwise you would not have gotten as far as you did.


3. When creating the $oauth_consumer below right before your last curl request, it looks like you're still referencing $consumer_key and $consumer_secret, but you've changed these at the top of your file to $cContactKey and $cContactSecret. If you adjust the code creating the new OAuthConsumer to pass your renamed variables as opposed to the one in the sample code, this should resolve your issue. Yours should appear as below.


//Create API Request

    $oauth_consumer = new OAuthConsumer($cContactKey, $cContactSecret);

    $authorization_token = new OAuthToken($access_token, $access_token_secret);

    $api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl);

I hope this helps, please let us know if you have any other questions regarding this. Thanks!

David J

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