400 Bad Request when getting access token

CLINTC50
Rookie
0 Votes

I get authorization codes without issue but when using the following code I only get the response below.

 

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

 The code I am using to call for the access token is below.

 

// Use cURL to get access token and refresh token
		$ch = curl_init();

		// Define base URL
		$base = 'https://authz.constantcontact.com/oauth2/default/v1/token';

		// Create full request URL
		$url = $base . '?code=' . $_GET['code'] . '&redirect_uri=' . urlencode(DOMAIN.'connect/constantcontact/') . '&grant_type=authorization_code';
		//echo $url;
		curl_setopt($ch, CURLOPT_URL, $url);

		// Set authorization header
		// Make string of "API_KEY:SECRET"
		$auth = $this->_clientID . ':' . $this->_clientSecret;
		//echo $auth;
		// Base64 encode it
		$credentials = base64_encode($auth);
		// Create and set the Authorization header to use the encoded credentials, and set the Content-Type header
		$authorization = 'Authorization: Basic ' . $credentials;
		curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded'));

		// Set method and to expect response
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		// Make the call
		$result = curl_exec($ch);
		curl_close($ch);
		
		print_r($result);

 

4 REPLIES 4
CLINTC50
Rookie
0 Votes

Any ideas on what could be causing this? We are following the docs exactly...

CLINTC50
Rookie
0 Votes

I solved this by adding the following to the post body

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['client_id' => $this->_clientID, 'client_secret' => $this->_clientSecret, 'code' => $_GET['code']]) );
Courtney_E
Employee
0 Votes

Hello CLINTC50, 

 

Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.

 

I'm glad to hear you were able to resolve your problem! 

 

If you run into any other issues going forward, please feel free to contact out team directly (if desired) at webservices@constantcontact.com 

 


Regards,

Courtney E.
Tier II API Support Engineer

Did I answer your question?
If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
user914495
Rookie
0 Votes

Still I am facing the same issue while generating the access token.2023-08-28 17_40_05-400 Bad Request.png

Resources
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up