【问题标题】:MapMyFitness API - can't get OAuth2 working in PHPMapMyFitness API - 无法让 OAuth2 在 PHP 中工作
【发布时间】:2014-09-24 00:50:01
【问题描述】:

全部,

我正在尝试使用 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。

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

【问题讨论】:

    标签: php json api oauth-2.0 mapmyfitness


    【解决方案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);

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

    【讨论】:

    • {"error": "invalid_grant"} 遇到此错误请帮助其紧急处理。
    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 2017-11-28
    • 2020-02-03
    • 2010-11-30
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多