MailChimp访问令牌CURL请求不起作用

时间:2016-02-12 00:33:31

标签: php api curl mailchimp

我正在尝试获取MailChimp访问令牌但有问题。我可以成功登录请求“代码”,但是当我去请求访问令牌时,我得到了一个奇怪的响应,例如:

HTTP/1.1 400 Bad Request
Server: nginx
Content-Type: application/json
Content-Length: 53
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store
Pragma: no-cache
X-UA-Compatible: IE=edge,chrome=1
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Fri, 12 Feb 2016 00:58:14 GMT
Connection: close
Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/
Set-Cookie: PHPSESSID=gjqtq7kelntldr82aonhpl3id3; expires=Fri, 12-Feb-2016 01:22:14 GMT; path=/; secure; HttpOnly
Set-Cookie: _TEST_COOKIE=9957289b5c4e0a63b4d5; path=/
Set-Cookie: PHPSESSDATA=2a45226783e34312753e142108bb48a8a0789b89%3A1455240134%3AeNoDAAAAAAE%3D; expires=Fri, 12-Feb-2016 01:22:14 GMT; path=/; domain=login.mailchimp.com; secure; httponly

这是我的代码:

$url = 'https://login.mailchimp.com/oauth2/token';
$client_id = 'xxxxxxxx';
$client_secret = 'xxxxxxxxx'; 
$redirect_uri = 'http://127.0.0.1/mailChimp/complete.php';
$code = $_GET['code'];
$fields = array(
        'grant_type' => 'authorization_code',
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'code' => $code,
        'redirect_URI' => $redirect_url
    );

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);   
curl_setopt($ch, CURLOPT_HEADER, TRUE);     
curl_setopt($ch, CURLOPT_TIMEOUT, TRUE);        
curl_setopt($ch, CURLOPT_USERAGENT, 'oauth2-draft-v10');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
// curl_setopt($ch, CURLOPT_ENCODING, "application/json");
$value = http_build_query($fields); //params is an array
curl_setopt($ch, CURLOPT_POSTFIELDS, $value);
$res=curl_exec($ch);
curl_close();
echo '<pre>';
print_r($res);
echo '</pre>';
die; 

我已浏览过这些文档:https://apidocs.mailchimp.com/oauth2/ 这些文档:http://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-oauth2/ 我根本无法得到足够的回应。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

redirect_URI更改为redirect_uri 这将解决您的问题。