英文:
ConstantContact V3 400 Error when getting access token
问题
以下是翻译的部分:
如果有人熟悉Constant Contact V3 API,也许你可以提供帮助。
我们正在按照这里的文档操作:https://developer.constantcontact.com/api_guide/server_flow.html
我甚至复制并粘贴了PHP代码,但仍然收到400错误,无法弄清楚如何修复它。
首先,我们使用此进行身份验证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/');
这个步骤有效,我们确实从Constant Contact那里收到了一个代码。
接下来,我们使用以下代码请求访问令牌:
// 使用cURL获取访问令牌和刷新令牌
$ch = curl_init();
// 定义基本URL
$base = 'https://authz.constantcontact.com/oauth2/default/v1/token';
// 创建完整的请求URL
$url = $base . '?code=' . $_GET['code'] . '&redirect_uri=' . urlencode(DOMAIN.'connect/constantcontact/') . '&grant_type=authorization_code';
curl_setopt($ch, CURLOPT_URL, $url);
// 设置授权头部
// 创建“API_KEY:SECRET”的字符串
$auth = $this->_clientID . ':' . $this->_clientSecret;
// 对其进行Base64编码
$credentials = base64_encode($auth);
// 创建并设置Authorization头部以使用编码凭据,并设置Content-Type头部
$authorization = 'Authorization: Basic ' . $credentials;
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded'));
// 设置请求方法和期望响应
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 进行调用
$result = curl_exec($ch);
curl_close($ch);
我们每次只收到一个400错误的响应:
<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
如何修复此问题将不胜感激,因为时间有点紧迫。
英文:
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.
答案1
得分: 1
已解决。在帖子正文中还需要以下字段。
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['client_id' => $this->_clientID, 'client_secret' => $this->_clientSecret, 'code' => $_GET['code']]) );
英文:
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']]) );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论