cancel
Showing results for 
Search instead for 
Did you mean: 

Need to hardcode access token in PHP

All Star

Need to hardcode access token in PHP

I have a script that is a form available to the public to fill out, submit, get some data analyzed and in the process check Constant Contact to see if the user exists. If not, it adds it, otherwise it skips trying to add it. This all works when I run through it and have manually clicked the Grant Access button but if I try another browser that doesn't have the same session or someone else entirely tries to fill out the form from another computer, they get the error "OAuth Exception: Username travis@drtraviselliott.com not found in datastore" and I have it setup to send me an email when that happens to click the link to Grant Access.  

 

This is not acceptable as the script just needs to ALWAYS have access to hit the CC system to check for and add a new contact.

 

I have read as many docs as I can find on your site but can't find the specific solution to my problem but it seems like hardcoding the access token would be the right solution. If so, where do I get the permanent access token?

 

include_once('../ConstantContact.php');
		
		$username = 'travis@drtraviselliott.com';
		$apiKey = '<REMOVED>';
		$consumerSecret = <REMOVED>';
		$consumerPassword = '<REMOVED>';
		$Datastore = new CTCTDataStore();
		$DatastoreUser = $Datastore->lookupUser($username);
		
		if($DatastoreUser){
			$ConstantContact = new ConstantContact('oauth', $apiKey, $DatastoreUser['username'], $consumerSecret);
			//$ConstantContact = new ConstantContact('basic', $apiKey, $username, $consumerPassword);
			$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>";
			
					}*/
				}
			
			}
			
		}
13 REPLIES
Honored Contributor

Re: Need to hardcode access token in PHP

Hello,

 

If you are working with a form that is specific to your own website, then you are correct that the best option would be to store the access token by hard-coding it or including it through a configuration file. You can easily get your Access Token here: https://constantcontact.mashery.com/io-docs

 

Simply copy & paste your API key into the API Key field and click Get Access token. This will walk you through the flow and at the end you will be presented with your Access Token. I do also want to mention that while it is not truly permanent, the Access Token currently persists for 10 years which should work well for you.

 

If you have any questions or problems, please let us know!

 

Sincerely,

Elijah G.
API Support Engineer
All Star

Re: Need to hardcode access token in PHP

Thanks for getting back to me Elijah. I entered my API Key and clicked Get Access Token and got the following on the next page:

 

=================

the client identified by : 2362a149-c9cb-4244-b8bd-be86d854491c does not have pre-registered redirect uri

Please close the window to return to your application.

=================

 

What do I do now?

Honored Contributor

Re: Need to hardcode access token in PHP

It looks like your API key (and likely also the code for your form) is for our V1 XML API rather than our newer V2 JSON API. If possible, I would strongly advise basing your form on our V2 API instead, as our V1 API is currently in a deprecated state and will be decommissioned(after several months of notice) in the future. For working with our V2 API, you could use our PHP SDK, which includes a sample form that you could easily adapt to your needs.

 

Regardless of if you choose to continue with our V1 API or work with our V2 API, you will want to create a new developer account and API key through Mashery(our partners for Key management) here: https://constantcontact.mashery.com/member/register The whole process should only take a few minutes and you will be able to use that key to get your Access Token. In addition, this new API key will be fully functional with both the V1 and V2 APIs.

 

Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
All Star

Re: Need to hardcode access token in PHP

Regarding V1 vs V2, currently it is not an option to revise the code for V2. That will have to be addressed at a later date.

 

I got the Access Token. In the line "new ConstantContact()" line of code, where do I put the Access Token:

 

$ConstantContact = new ConstantContact('oauth', $apiKey, $DatastoreUser['username'], $consumerSecret);
Honored Contributor

Re: Need to hardcode access token in PHP

To use your code with OAuth2 and the access token, you can do the following:

 

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

 

If you have any difficulties getting that up and running, let me know!

 

Sincerely,

Elijah G.
API Support Engineer
All Star

Re: Need to hardcode access token in PHP

Are oauth and oath2 interchangeable in this case? And where do I find my CTCT_USERNAME? Would it be the same username as $DatastoreUser['username']?

Honored Contributor

Re: Need to hardcode access token in PHP

Hello,

 

"oauth" and "oauth2" are not interchangeable, as one indicates OAuth 1, and the other indicates OAuth 2, which have some significant differences.

 

Since you are hard-coding the Access Token, you will not be using the OAuth process and therefore will not be making use of the datastore tool. Because of this, the username must also be hard-coded. The username you need is the same username that is used to log in to the Constant Contact account which you wish to connect. It should also be the same username that was being placed in $DatastoreUser['username'].

 

Let me know if you have any additional questions!

 

Sincerely,

Elijah G.
API Support Engineer
All Star

Re: Need to hardcode access token in PHP

So to be clear, I am currently using oauth, not oauth2. Can I use oauth with the CTCT_USERNAME and Access Token I generated?

Honored Contributor

Re: Need to hardcode access token in PHP

Hello,

 

Thanks for the clarification. In your case it will be notably simpler if you were to use OAuth 2 rather than OAuth 1. In order to user OAuth 2, you should only need the Access Token that you generated, your username, and new your API Key. If you want to use OAuth 1, it is more involved to hardcode the token, and not as well supported by the wrapper code that you are using.

 

Is there a specific reason to prefer OAuth 1 over OAuth 2?

 

Sincerely,

Elijah G.
API Support Engineer
Still need help?
You can post a new message in the Community or find us on Twitter Mon-Fri 8am - 8pm ET. We've got real people waiting to help you out. Click below to start a conversation!