$apiKey = ""; $consumerSecret = ""; $redirectURI = ""; $code = $_REQUEST['code']; $username = $_REQUEST['username']; //We will use PHP cURL to make a request to Constant Contact to get the Access Token. $rqurl = "https://oauth2.constantcontact.com/oauth2/oauth/token?grant_type=authorization_code&client_id={$apiKey}&client_secret={$consumerSecret}&code={$code}&redirect_uri={$redirectURI}"; $rq = curl_init(); curl_setopt($rq, CURLOPT_URL, $rqurl); curl_setopt($rq, CURLOPT_RETURNTRANSFER, 1); curl_setopt($rq, CURLOPT_BINARYTRANSFER, 1); curl_setopt($rq, CURLOPT_HEADER, 0); curl_setopt($rq, CURLOPT_SSL_VERIFYPEER, 0); if (!($result = curl_exec($rq))) { //If there is an error, dump it so we can see it. echo curl_error($rq); } else { echo "<pre>Access Token Information:\n"; print_r($result); $obj = json_decode($result); // The Result of this request is returned as JSON, so we need to parse it. //Create the User Ojbect. $user['username'] = $username; $user['access_token'] = $obj->access_token; //Use the DataStore to store the User in the PHP Session. $Datastore->addUser($user); //Create a link back to index.php where we can now use the Session Data. echo "\n<a href='redirect_uri'>Click Here to Continue</a></pre>"; //Replace redirect_uri with your index.php full URL. } curl_close($rq);
<?php include_once '../ConstantContact.php'; session_start(); // Set variables $api_key = $_GET['apiKey']; // API Key $consumer_secret = $_GET['secret']; // Consumer Secret $_SESSION['return'] = $_GET['return']; $callback_url = 'http://' . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] != '80' ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['REQUEST_URI']; // Instantiate ConstantContact class with the key and secret from ConstantContact.php $CTCTOAuth = new CTCTOAuth($api_key, $consumer_secret, $callback_url); if (!$_GET['oauth_verifier']) { $CTCTOAuth->getRequestToken(); $_SESSION['request_token'] = $CTCTOAuth->request_token->key; $_SESSION['request_secret'] = $CTCTOAuth->request_token->secret; header('Location: ' . $CTCTOAuth->generateAuthorizeUrl()); } else { $returnLocation = 'http://' . $_SESSION['return']; unset($_SESSION['return']); $requestToken = new OAuthToken($_SESSION['request_token'], $_SESSION['request_secret']); $CTCTOAuth->request_token = $requestToken; $CTCTOAuth->username = $_GET['username']; $CTCTOAuth->getAccessToken($_GET['oauth_verifier']); $sessionConsumer = array('key' => $CTCTOAuth->access_token->key, 'secret' => $CTCTOAuth->access_token->secret, 'username' => $CTCTOAuth->username); $Datastore = new CTCTDataStore(); $Datastore->addUser($sessionConsumer); header('Location: ' . $returnLocation); }