获取Mailchimp的访问令牌时出错。返回{“error”:“invalid_grant”}

时间:2017-07-12 20:30:57

标签: php oauth-2.0 mailchimp-api-v3.0

得到一个代码,我试图为它获取访问令牌。我发出了一个Curl请求并执行了它但只得到了错误响应{{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;

1 个答案:

答案 0 :(得分:1)

From the documentation:

  

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

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