0

全部,

我正在尝试使用 MapMyFitness 的 API 和 OAuth2。

这是我正在使用的精简代码(类似于我成功用于连接 Strava 和 RunKeeper API 的代码):

$mapMyRun_authorization_code = "*****";
$client_id = "*********";
$client_secret = "*************";

$url="https://oauth2-api.mapmyapi.com/v7.0/oauth2/access_token/";

$postfields = array(
    "grant_type" => "authorization_code",
    "client_id" => $client_id,
    "client_secret" => $client_secret,
    "code" => $mapMyRun_authorization_code
);

$headers = array('Api-Key: ' . $client_id);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$json = curl_exec ($ch);
$responsecode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

当我运行这个 -$json是空的,$responsecode是 400,而不是 200。

我的请求显然做错了什么,但我不知道是什么......

4

1 回答 1

1

因此,感谢 MapMyRun 团队的帮助,答案如下:

$mapMyRun_authorization_code = "*****";
$client_id = "*********";
$client_secret = "*************";

$url="https://oauth2-api.mapmyapi.com/v7.0/oauth2/access_token/";

$postfields = "grant_type=authorization_code&code=" . $mapMyRun_authorization_code . "&client_id=". $client_id . "&client_secret=" . $client_secret;

$headers = array('Api-Key: ' . $client_id);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$json = curl_exec ($ch);
$responsecode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

重要的变化 -$postfields 不能是一个数组,你不应该包括 curl_setopt($ch, CURLOPT_POST, true);

希望这对其他人有帮助......

于 2014-08-01T12:38:21.127 回答