10-07-2015 07:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-07-2015 07:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I am creating a Laravel App in which I need to access the FitBit API to allow users steps, distance etc to be shown.
I have tried many different OAuth libraries but none of them can get past the authorization to access token stage of authentication.
Is there an SDK or library I can use to implement this as I have found nothing in the past week I have been trying to get this to work.
Thanks
Answered! Go to the Best Answer.
Accepted Solutions
10-08-2015 07:38 - edited 10-08-2015 07:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



10-08-2015 07:38 - edited 10-08-2015 07:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
It looks like you're sending the HTTP body contents as a header instead of body. Try sending the body contents with CURLOPT_POSTFIELDS
.
10-07-2015 08:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



10-07-2015 08:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Fitbit uses standard OAuth 1.0a or OAuth 2.0, so any OAuth library should be able to work with Fitbit.
If you're using OAuth 2.0 (recommended), you don't really need a library. You can just make the HTTP requests with an Authorization: header. The Authorization Code Grant Flow is fairly straightforward. Check it out at https://dev.fitbit.com/docs/oauth2/#authorization-code-grant-flow

10-07-2015 08:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-07-2015 08:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I have been trying to find out how to make authorization requests in Laravel as it doesn't seem to support it at all. A lot of people have recommended Guzzle for the requests but guzzle doesn't allow you to send requests in the format the API requires.
Is there an easier way to send these requests?

10-07-2015 08:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



10-07-2015 08:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
A quick search suggests Laravel is a PHP framework. Have you tried PHP's cURL library for making HTTP requests?

10-08-2015 00:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-08-2015 00:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Just created a CURL request for this and i'm getting this error: http://imgur.com/tUoM4ov which seems odd because I definitely include the grant type in the request. Do you know what I could be doing wrong to get this error as I definitely put the grant type in?
My Token Function:
public function getToken($request_url, $client_id, $client_secret, $code, $redirect_uri){ // base64 encode the client_id and client_secret $auth = base64_encode("{$client_id}:{$client_secret}"); // urlencode the redirect_url $redirect_uri = urlencode($redirect_uri); // Set the headers $headers = [ "Authorization: Basic {$auth}", "Content-Type: application/x-www-form-urlencoded", "\n\r", "client_id={$client_id}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}" ]; // Initiate curl session $ch = curl_init(); // Set headers curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Options (see: http://php.net/manual/en/function.curl-setopt.php) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_POST, 1); // Execute the curl request and get the response $response = curl_exec($ch); // Throw an exception if there was an error with curl if($response === false){ throw new Exception(curl_error($ch), curl_errno($ch)); } // Get the body of the response $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $responseBody = substr($response, $header_size); // Close curl session curl_close($ch); // Return response body return $responseBody; }
My Controller:
try
{
$fitbitConnection = new FitBit();
$token_response = $fitbitConnection->getToken("https://api.fitbit.com/oauth2/token", $client_id, $secret, $code, $redirect);
echo $token_response;
}
catch(Exception $e)
{
// curl error
echo $e->getMessage();
}

10-08-2015 07:38 - edited 10-08-2015 07:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



10-08-2015 07:38 - edited 10-08-2015 07:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
It looks like you're sending the HTTP body contents as a header instead of body. Try sending the body contents with CURLOPT_POSTFIELDS
.
10-08-2015 07:43 - edited 10-08-2015 07:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-08-2015 07:43 - edited 10-08-2015 07:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I noticed this in the API Docs an hour or so ago. It pays to read things extremely carefully. Thanks for your help.

