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.

Adding contact via API - PHP

Highlighted
Occasional Participant

Adding contact via API - PHP

Hi -

 

we are trying the file addContact.php from the API examples and trying to add a contact remotelly.

 

Whenever i open the file... i get this error.

 

OAuth Exception: Username jmextensions not found in datastore Click here to authorize


Is there a way to exclude this...

what we are trying to do is add a contact to a predefined list id and name.

 

We will be doing a plugin and giving out to users and users will just be changing the username, consumer secret, APIkey.

 

Thank you to anyone who responds.

7 REPLIES 7
Highlighted
Moderator

Re: Adding contact via API - PHP

What that error indicates is that you are using OAuth 2.0 authentication with the wrapper but have stored the OAuth credentials in the session so that the library knows they exist.  If you are doing this, you'll need to have make sure that you have started the session and loaded the correct data from either your server storage, a database or through hard coding it.  Here's an example of what it would look like to hard code it:

 

 $ConstantContact = new ConstantContact ( "oauth2", $apikey, $username, $accesstoken);
 
 $Datastore = new CTCTDataStore ();
 $Datastore->addUser ( array ('username' => $username, 'access_token' =>  $accesstoken) );

 

That will set up the user in the session (or just the single PHP page).

 

If you are planning on selling/distributing you integration, I would recommend adding the following changes to your flow:

 

1. Hard code your API key (our customers do not use API keys, that's only used to signify a developer).  If you make our customers get an API key, it will cause frustration, confusion and abandonment of your plugin.  

2. Implement a self-managed OAuth 2.0 flow that allows the user to grant access to your plugin and then stores/loads the OAuth token and username from a database/storage on their server.  We have put in stub methods in the CTCTDataStore class for doing just this, you'll just need to implement those methods for your integration.

3. If you are planning on selling/distributing this, you'll have the opportunity to get new customers for Constant Contact.  I'd recommend applying for our partner program as well so that if you do get referrals for new customers, you will be elligible for revenue sharing opportunities.  You can apply for that program here: http://www.constantcontact.com/partners/technology-platform-partners.jsp

 

Hope this helps!

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Adding contact via API - PHP

Dave -

 

my understanding was that all our users would need to do it drop in thier username (also you told me API is not required each user.) and they should be good to go.

but... how do we go for Oauth at all.

 

is this like facebook that each account has to first authenticate a application before it can start using it and the authentication is something you don't save on your end... it's something on our end??? right?

 

Thanks

Highlighted
Moderator

Re: Adding contact via API - PHP

That's exactly the right take, it's just like getting a Facebook access token through OAuth.  Your app must get permission from the user to access their data, part of the access token we send back is their username.  OAuth 2.0 is built into the wrapper library and a sample of how to use it can be found in the github wiki pages.  If you run into any problems or questions, let us know and we can help out.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Adding contact via API - PHP

Ok -

 

here is the process i am thinking through... correct me i am wrong.

 

1. users downoad the plugin/script from out website.

2. we give them the instructions and then need to link thier account to our application by clicking on a link to our site.

2.a link takes user to the OAuth login on constant contact and then return a token after sucessfull login.

3. we save the users username and returned token from Constant Contact into a db or file (whatever?).

4. upon isntallation of script/plugin on users website... we do a curl request to our site's script verifying that the username has a valid token and provide that in return to the Class you described in your post earlier to complete the fetching.

 

right?

Highlighted
Occasional Participant

Re: Adding contact via API - PHP

Ok...

i did a return url and it gave me a post back with code and username.

 

now, as i have the code... i am trying to do this locally without storing it in the db first.

 

i wrote the variable.

$code = 'xxxx';
$accessToken = 'xxx'; in the addContact.php file as i am now sure which variable will work for me... but it's still not working... my best gues... these variables are not being used.

as seen in the code below.

 

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

if($DatastoreUser){
    $ConstantContact = new ConstantContact('oauth', $apiKey, $DatastoreUser['username'], $consumerSecret);
    $ContactLists = $ConstantContact->getLists();

 

any tips?

Highlighted
Occasional Participant

Re: Adding contact via API - PHP

sorry if i buggin you with too many messages.

 

but... even if i use the code.

 

 $ConstantContact = new ConstantContact ( "oauth2", $apikey, $username, $accesstoken);
 
 $Datastore = new CTCTDataStore ();
 $Datastore->addUser ( array ('username' => $username, 'access_token' =>  $accesstoken) );

 

i still get the same "Click here to authorize."

Highlighted
Trusted Contributor

Re: Adding contact via API - PHP

No bother at all. This is what the forum is for.

 

The code that is sent back after your authrize through out login screen and allow access is not the access token that can be used to authenticate subsequent API Requests.  In the OAuth 2. server flow, this code should be recieved by your authentication script, and then another call should be made to our servers to exchange this code for the access token, which our server will return in it's response.  Getting the token involves:

 

1) redirect to our authorization page to login and grant access to an account.

2) recieve the access code that is provided when our system redirects back to your redirect url (verification url)

3) make another request, providing the code recieved in exchange for the access token, which is provided in the JSON returned.

 

This is the access token you will need to use.

 

I hope this helps.  Let us know if you have additional questions.

Mark Coleman
Support Engineer
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