ConstantContact V3 在获取访问令牌时出现 400 错误。

huangapple go评论53阅读模式
英文:

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 = &quot;https://authz.constantcontact.com/oauth2/default/v1/authorize&quot;;
$authURL = $baseURL . &quot;?client_id=&quot; . $this-&gt;_clientID . &quot;&amp;response_type=code&amp;scope=&quot;.urlencode(&#39;offline_access&#39;).&quot;&amp;state=&quot; . $this-&gt;user-&gt;info[&#39;id&#39;] . &quot;&amp;redirect_uri=&quot; . urlencode(DOMAIN.&#39;connect/constantcontact/&#39;); 

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 = &#39;https://authz.constantcontact.com/oauth2/default/v1/token&#39;;

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

		// Set authorization header
		// Make string of &quot;API_KEY:SECRET&quot;
		$auth = $this-&gt;_clientID . &#39;:&#39; . $this-&gt;_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 = &#39;Authorization: Basic &#39; . $credentials;
		curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, &#39;Accept: application/json&#39;, &#39;Content-Type: application/x-www-form-urlencoded&#39;));

		// 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.

&lt;html&gt;
&lt;head&gt;&lt;title&gt;400 Bad Request&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;h1&gt;400 Bad Request&lt;/h1&gt;&lt;/center&gt;
&lt;hr&gt;&lt;center&gt;cloudflare&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;

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([&#39;client_id&#39; =&gt; $this-&gt;_clientID, &#39;client_secret&#39; =&gt; $this-&gt;_clientSecret, &#39;code&#39; =&gt; $_GET[&#39;code&#39;]]) );

huangapple
  • 本文由 发表于 2023年2月13日 23:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定