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

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

ConstantContact V3 400 Error when getting access token

问题

以下是翻译的部分:

如果有人熟悉Constant Contact V3 API,也许你可以提供帮助。

我们正在按照这里的文档操作:https://developer.constantcontact.com/api_guide/server_flow.html

我甚至复制并粘贴了PHP代码,但仍然收到400错误,无法弄清楚如何修复它。

首先,我们使用此进行身份验证URL:

  1. $baseURL = "https://authz.constantcontact.com/oauth2/default/v1/authorize";
  2. $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那里收到了一个代码。

接下来,我们使用以下代码请求访问令牌:

  1. // 使用cURL获取访问令牌和刷新令牌
  2. $ch = curl_init();
  3. // 定义基本URL
  4. $base = 'https://authz.constantcontact.com/oauth2/default/v1/token';
  5. // 创建完整的请求URL
  6. $url = $base . '?code=' . $_GET['code'] . '&redirect_uri=' . urlencode(DOMAIN.'connect/constantcontact/') . '&grant_type=authorization_code';
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. // 设置授权头部
  9. // 创建“API_KEY:SECRET”的字符串
  10. $auth = $this->_clientID . ':' . $this->_clientSecret;
  11. // 对其进行Base64编码
  12. $credentials = base64_encode($auth);
  13. // 创建并设置Authorization头部以使用编码凭据,并设置Content-Type头部
  14. $authorization = 'Authorization: Basic ' . $credentials;
  15. curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded'));
  16. // 设置请求方法和期望响应
  17. curl_setopt($ch, CURLOPT_POST, true);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. // 进行调用
  20. $result = curl_exec($ch);
  21. curl_close($ch);

我们每次只收到一个400错误的响应:

  1. <html>
  2. <head><title>400 Bad Request</title></head>
  3. <body>
  4. <center><h1>400 Bad Request</h1></center>
  5. <hr><center>cloudflare</center>
  6. </body>
  7. </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

  1. $baseURL = &quot;https://authz.constantcontact.com/oauth2/default/v1/authorize&quot;;
  2. $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

  1. // Use cURL to get access token and refresh token
  2. $ch = curl_init();
  3. // Define base URL
  4. $base = &#39;https://authz.constantcontact.com/oauth2/default/v1/token&#39;;
  5. // Create full request URL
  6. $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;;
  7. //echo $url;
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. // Set authorization header
  10. // Make string of &quot;API_KEY:SECRET&quot;
  11. $auth = $this-&gt;_clientID . &#39;:&#39; . $this-&gt;_clientSecret;
  12. //echo $auth;
  13. // Base64 encode it
  14. $credentials = base64_encode($auth);
  15. // Create and set the Authorization header to use the encoded credentials, and set the Content-Type header
  16. $authorization = &#39;Authorization: Basic &#39; . $credentials;
  17. curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, &#39;Accept: application/json&#39;, &#39;Content-Type: application/x-www-form-urlencoded&#39;));
  18. // Set method and to expect response
  19. curl_setopt($ch, CURLOPT_POST, true);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. // Make the call
  22. $result = curl_exec($ch);
  23. curl_close($ch);

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

  1. &lt;html&gt;
  2. &lt;head&gt;&lt;title&gt;400 Bad Request&lt;/title&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;center&gt;&lt;h1&gt;400 Bad Request&lt;/h1&gt;&lt;/center&gt;
  5. &lt;hr&gt;&lt;center&gt;cloudflare&lt;/center&gt;
  6. &lt;/body&gt;
  7. &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

已解决。在帖子正文中还需要以下字段。

  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.

  1. 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:

确定