PHP Classes

FitBit Offline Access For User

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How Can the PHP OAuth...  >  All threads  >  FitBit Offline Access For User  >  (Un) Subscribe thread alerts  
Subject:FitBit Offline Access For User
Summary:FitBit Offline Access For User
Messages:4
Author:primeapple
Date:2017-10-25 11:05:09
 

  1. FitBit Offline Access For User   Reply   Report abuse  
Picture of primeapple primeapple - 2017-10-25 11:05:09
Hi,

Great class and the Fitbit2 example works great, returning an access_token, refresh_token and userId accordingly following authentication.

However, when I try to access data on behalf of the user (no interaction) I get an error:

Error: it was not possible to access the API call: it was returned an unexpected response status 401 Response: {"errors":[{"errorType":"invalid_client","message":"Invalid authorization header format. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}

I'm setting the client_id, client_secret, redirect_uri, etc. and then call $client->Initialize() and NOT call $client->Process();

I set the $client->access_token to my stored token, set $client->offline to true and then CallAPI() with the endpoint I require.

However, I then get the error above so can you please help with what I'm missing?

BTW I don't get access_token_secret from the first authentication call if that helps.

Thanks!

  2. Re: FitBit Offline Access For User   Reply   Report abuse  
Picture of primeapple primeapple - 2017-10-25 11:18:02 - In reply to message 1 from primeapple
OK, I found the solution!

I needed to add $client->access_token_type = 'Bearer'; to my code before calling CallAPI().

Thanks once again for a great library.

  3. Re: FitBit Offline Access For User   Reply   Report abuse  
Picture of Yaliang Chuang Yaliang Chuang - 2018-02-04 21:49:14 - In reply to message 2 from primeapple
Hello, primeapple, could you share your solution? I want to use the saved token to retrieve the data from Fitbit, too. I first used the authentication function and successfully obtained and save the token in a MySQL database. Then, I followed your message to create a php script to call the function. My code is as follows:
------------------
if ($savedToken != null){
$savedTokenString = mysql_fetch_object($savedToken)->token;

$client->Initialize();
$client->access_token = $savedTokenString;
$client->offline = true;
$client->access_token_type = 'Bearer';

$method = 'GET';
$parameters = array();
$options = array();
$success = $client->CallAPI($apiCall, $method, $parameters, $options, $response);

echo '<pre>' . print_r($response, TRUE) . '</pre>';

$client->Finalize($success);
----------------------
I got an error of "expired_token." Should I refresh the token before calling the API?
-------------
[errorType] "expired_token
[message] => Access token expired: [Token]. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process.
--------------

Could you share your code with me?

Thank you very much.

Best Regards,

Yaliang

  4. Re: FitBit Offline Access For User   Reply   Report abuse  
Picture of Renae Renae - 2018-04-19 01:46:00 - In reply to message 2 from primeapple
Do you still have your code for this solution, I would love to see it? When I run mine nothing is outputed to my json files. My client->access_token is hardcoded into the php for testing. I plan to store it in a database later on. Any help is appreciated.