得到了一个代码,我试图为它获取访问令牌。我做了一个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;
这可能有所帮助:https://stackoverflow.com/questions/12724581/mailchimp-oauth2-in-asp-net-keep-returning-invalid-grant – aynber