cancel
Showing results for 
Search instead for 
Did you mean: 

Need to hardcode access token in PHP

Occasional Participant

Re: Need to hardcode access token in PHP

When I originally wrote the script I tried implementing oauth2 and could not get it to work. I ended up working off another of your code examples that used oauth1 and was able to get it working pretty quickly except I didn't realize at the time that it was only working on a per session basis not an application basis after I had clicked the Grant Access button but I see now that using another browser or anyone else at all going to the page and submitting the form is going to get the oauth error. 

 

A full rewrite of the code is definitely not an ideal option at this point. Referring to the original code snippet I sent, is there a small tweak I can make to accomplish the auth by access token and if not, can you point me in the direction of example code that will accomplish what I need relatively easily?

Honored Contributor

Re: Need to hardcode access token in PHP

Hi Travis,

 

you should be able to accomplish both of your goals (Oauth 2, and hardcoding the access token) by simply using this snippet when intiializing the ConstantContact object:

$ConstantContact = new ConstantContact('oauth2', $apiKey, 'CTCT_USERNAME', 'ACCESS_TOKEN');

Simply replace the two placeholders in that code with the appropriate values, and you should be able to immediately connect to and use the API with OAuth2 and the access token. If you run into any issues with that, please let me know!

 

Sincerely,

 

 

 

Elijah G.
API Support Engineer
Occasional Participant

Re: Need to hardcode access token in PHP

I updated my code as follows and am still getting the same error: OAuth Exception: Username travis@drtraviselliott.com not found in datastore

 

include_once('/..ConstantContact.php');
		
		$username = 'REMOVED';
		$apiKey = 'REMOVED';
		$consumerSecret = 'REMOVED';
		$accessToken = 'REMOVED';
		$Datastore = new CTCTDataStore();
		$DatastoreUser = $Datastore->lookupUser($username);
		
		if($DatastoreUser){
			$ConstantContact = new ConstantContact('oauth2', $apiKey, $username, $accessToken);
			$ContactLists = $ConstantContact->getLists();
        		
			if(isset($youremail)){
			
				$contactSearch = $ConstantContact->searchContactsByEmail($youremail);
				
				if(!$contactSearch){
					
					$parts = explode(" ", $yourname);
					$lastname = array_pop($parts);
					$firstname = implode(" ", $parts);
					
					$Contact = new Contact();
					$Contact->emailAddress = $youremail;
					$Contact->firstName = $firstname;
					$Contact->lastName = $lastname;
					$Contact->lists = $_POST['lists'];
			
					$NewContact = $ConstantContact->addContact($Contact);
					/*if($NewContact){
						echo "Contact Added. This is your newly created contact's information<br /><pre>";
						print_r($NewContact);
						echo "</pre>";
			
					}*/
				}
			
			}
			
		}
Honored Contributor

Re: Need to hardcode access token in PHP

Since we're hardcoding both the username and the access token, you can remove this datastore code:

$Datastore = new CTCTDataStore();
$DatastoreUser = $Datastore->lookupUser($username);

That should resolve the problem. Let me know if there's any further roadblocks!

 

Sincerely,

Elijah G.
API Support Engineer