All,
I'm attempting to use MapMyFitness' API and OAuth2.
Here's the stripped down code I'm using (similar to code I've used successfully to connect with Strava's and RunKeeper's 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);
When I run this - $json
is empty, and $responsecode
is 400, not 200.
I'm obviously doing something wrong with my request, but I have no idea what...
So, thanks to help from the MapMyRun team, here's the answer:
$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);
The important changes - $postfields
cannot be an array, and you should not include curl_setopt($ch, CURLOPT_POST, true);
Hopefully this will be helpful to someone else...