我想从 mailchimp oauth2 获取访问令牌,但在每次尝试中我都失败了。在这里我使用了guzzle,我的实际要求是
卷曲 --请求开机自检 \--网址 'https://login.mailchimp.com/oauth2/token' \--data "grant_type=authorization_code&client_id={client_id}&client_secret={client_secret}&redirect_uri={encoded_url}&code={code}" \--包括
我已经尝试了 curl 和 JavaScript,但 JavaScript 请求不被 MailChimp 接受这是我使用的代码:
$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2',
'defaults' => [
'exceptions' => false ],
'header' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
],
'verify' => false
]);
$result = $http->request('POST','/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $this->redirect_uri,
'code' => $code,
],
]);
$response = $result->send();
$responseBody = $response->getBody(true);
var_dump($responseBody);
die();
预期的实际结果是:"access_token":"5c6ccc561059aa386da9d112215bae55","expires_in":0,"scope":null但是我的错误客户端错误: POST https://login.mailchimp.com/token
导致了404 Not Found
的回应:
由于 Guzzle 的工作方式,您使用的是绝对 URI。
首先,设置base_uri
:
$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2']);
然后提出请求:
$http->request('POST', '/token')
因为您添加了前导斜杠,所以 Guzzle 会将其视为具有绝对 URI 并命中https://login.mailchimp.com/token
,就像它所做的那样。我的猜测是期望命中的端点是https://login.mailchimp.com/oauth2/token
可以通过向base_uri
添加尾部斜杠并在执行请求时删除前导斜杠来解决此问题。
来自文档的信息:http://docs.guzzlephp.org/en/stable/quickstart.html#creating-a-client
它遵循RFC3986