2017-07-12 69 views
0

得到了一个代码,我试图为它获取访问令牌。我做了一个Curl请求并执行它,但只有错误响应为{“error”:“invalid_grant”}。这里是我的代码:为Mailchimp获取访问令牌时出错。返回{“error”:“invalid_grant”}

$url = 'https://login.mailchimp.com/oauth2/token'; 
    $grant_type = 'authorization_code'; 
    $client_id = 'MY CLIENT ID'; 
    $client_secret = 'SECRET KEY'; 
    $redirect_url = urlencode('REDIREDT_URL'); 
    $code = $_GET['code'];; 

    $post_field = array(
     'grant_type' => 'authorization_code', 
     'client_id' => $client_id, 
     'client_secret' => $client_secret, 
     'redirect_uri' => $redirect_url, 
     'code' => $code, 
     ); 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_ENCODING, ''); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
    curl_setopt($ch, CURLOPT_POST, true); 
    $value = http_build_query($post_field); 
    //echo $value; exit; 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $value); 

    //echo $post_field; 
    $result = curl_exec($ch); 
    curl_close($ch); 
    print_r($result); exit; 
+0

这可能有所帮助:https://stackoverflow.com/questions/12724581/mailchimp-oauth2-in-asp-net-keep-returning-invalid-grant – aynber

回答

1

From the documentation:

invalid_grant 所提供的授权许可(例如授权 码,资源所有者凭证)或刷新令牌就是 无效,过期,撤消不匹配在授权请求中使用的重定向 URI,或者发给 另一个客户端。

我会获得一个新的授权令牌。

+0

在评论中的手册链接将达到相同的这 – RiggsFolly

+0

谢谢你的回复:)但我已经加倍检查我的凭据,发现他们是正确的。有一件事让我感到不安,就是'带外'请求。你有什么想法如何做出带外请求。 –

相关问题