1

If anyone is familiar with the Constant Contact V3 API perhaps you can be of help.

We are following the docs here: https://developer.constantcontact.com/api_guide/server_flow.html

I have even copy and pasted the PHP code but am Still receiving a 400 error and can't figure out what to do to fix it.

1st we use this for the auth url

$baseURL = "https://authz.constantcontact.com/oauth2/default/v1/authorize";
$authURL = $baseURL . "?client_id=" . $this->_clientID . "&response_type=code&scope=".urlencode('offline_access')."&state=" . $this->user->info['id'] . "&redirect_uri=" . urlencode(DOMAIN.'connect/constantcontact/'); 

This works and we do receive a code from constant contact. Next we use the following code to ask for an access token

// 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);

The response we receive is just a 400 error every time.

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

Any help or insight on how to fix this would be greatly appreciated. In a bit of a time pinch.

Clint C.
  • 678
  • 13
  • 31

1 Answers1

1

Solved. The following fields were also needed in the post body.

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['client_id' => $this->_clientID, 'client_secret' => $this->_clientSecret, 'code' => $_GET['code']]) );
Clint C.
  • 678
  • 13
  • 31
  • Just when I thought CC's API couldn't get any more convoluted. Thank you very much, no way I would have found this out on my own. – naturaljoin Apr 12 '23 at 07:59