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.

How does CTCTOauth2 work in the examples

Highlighted
Occasional Contributor

How does CTCTOauth2 work in the examples

As far as I can tell the CTCTOauth2 class never actually does the initial authentication, it only gets the results of the authentication that it never runs.  is this correct?  I can litterally get basic authentication to work in two seconds but I can't get this to work at all.

 

class CTCTOauth2 {
	public $base_uri = "https://oauth2.constantcontact.com/oauth2/oauth/token?grant_type=authorization_code&client_id=";
	public $api_key;
	public $consumer_secret;
	public $url;
	public $code;
	
	function __construct($apikey, $consumersecret, $redirect, $thecode)
	{
		$this->api_key = $apikey;
		$this->consumer_secret = $consumersecret;
		$this->url = urlencode($redirect);
		$this->code = $thecode;
	}
	
	function getAccessToken()
	{
		$request = $this->base_uri . $this->api_key . 
		"&client_secret=" . $this->consumer_secret . 
		"&code=" . $this->code.
 		"&redirect_uri=" . $this->url;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $request);
		
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
		
		$result = curl_exec($ch);
		curl_close($ch);
		
		print_r($result);
		$obj = json_decode($result);
		
		return $access_token = $obj->access_token;
	}
}

 

10 REPLIES 10
Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

Sorry I missunderstood how it worked.  When I just reverted everything back to it's original state when I try and login it takes me to a login screen then when I log in is says:

 

HTTP Status 404 -


type Status report

message

description The requested resource () is not available.


JBoss Web/2.1.1.SNAPSHOT

Highlighted
Employee

Re: How does CTCTOauth2 work in the examples

Hi,

 

The 404 would most likely indicate that the redirect url you are going to isn't available.  Can you get to your redirect url normally through your browser?

 

It's worth mentioning that the redirect URI that you specify in the config.php file has to match what is stored in Constant Contact, which you can view by going to this link, and logging in to view the details of your API Key.

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

Here's the thing it's not getting that far here's what in the address bar when it errors out. 

 

https://oauth2.constantcontact.com/oauth2/oauth/{{My Username}}/authorize?response_type=code&client_id={{Myapikey}}&redirect_uri=http%3A%2F%2Fallflo.local%3A85%2Fclasses%2FConstantContact%2Fexample_verification.php.

 

When you say then need to match do you mean before or after the encoding?  For example should the api be

http://allflo.local

or

http%3A%2F%2Fallflo.local

Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

Scratch the second question, it won't let me input it as a encoded string.

Highlighted
Employee

Re: How does CTCTOauth2 work in the examples

Hi,

 

I would like to send you the files that I use for OAuth 2 authentication to see if you can follow those.  You will change a few things in the files, adding your API Key and other specific credentials (like redirect URI).  It seems like people are running into issues with the examples we currently provide, so I'm hoping my files will help.  Can you email me at webservices@constantcontact.com so that I can give you a zip file to unzip and use?

 

Thank you for contacting Constant Contact, and have a wonderful day!

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

Ok that sounds good.

Highlighted
Employee

Re: How does CTCTOauth2 work in the examples

Hi,

 

I haven't received any emails from you yet, but if you'd like, you can download the files I use to authenticate at my repository here.  Let me know how it goes.

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

Not it says invalid postback but now it works better thank you.

Highlighted
Occasional Contributor

Re: How does CTCTOauth2 work in the examples

I forgot the change the validation url in the api key.  Now it works.  Thank you.

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