Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

The API you are requesting could not be found

ANSWERED

Hello Fitbit Support,

 

I am new to Fitbit community.  I am currently working on integrating Fitbit into our application using PHP.  I have successfully authorized the users and generated access and refresh tokens for making API calls to get the data associated with their Fitbit account.

 

The problem that I am currently running into is that I am unable to make calls to certain APIs using the access token.  However using the same access token I am able to call other APIs and get the results successfully.

 

For example, the following API call is successful:

 

API : https://api.fitbit.com/1/user/-/activities/date/today.json

 

Result: 

HTTP/1.1 100 Continue HTTP/1.1 200 OK
Server: cloudflare-nginx Date: Thu, 17 Dec 2015 08:55:35 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-control: no-cache, private
Fitbit-Rate-Limit-Limit: 150
Fitbit-Rate-Limit-Remaining: 144
Fitbit-Rate-Limit-Reset: 265
Content-Language: en
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
CF-RAY: 256168ea457f2db5-BOM

 

{"activities":[],"goals":{"activeMinutes":30,"caloriesOut":2450,"distance":8.05,"steps":10000},"summary":{"activeScore":-1,"activityCalories":0,"caloriesBMR":828,"caloriesOut":828,"distances":[{"activity":"total","distance":0},{"activity":"tracker","distance":0},{"activity":"loggedActivities","distance":0},{"activity":"veryActive","distance":0},{"activity":"moderatelyActive","distance":0},{"activity":"lightlyActive","distance":0},{"activity":"sedentaryActive","distance":0}],"fairlyActiveMinutes":0,"lightlyActiveMinutes":0,"marginalCalories":0,"sedentaryMinutes":865,"steps":0,"veryActiveMinutes":0}}

 

However the following API call fails:

 

API: https://api.fitbit.com/1/user/-/activities/steps/date/today/7d.json

 

Result:

 

HTTP/1.1 100 Continue HTTP/1.1 404 Not Found
Server: cloudflare-nginx Date: Thu, 17 Dec 2015 08:53:20 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
CF-RAY: 2561659e84942ddf-BOM

 

{"errors":[{"errorType":"not_found","fieldName":"n/a","message":"The API you are requesting could not be found."}],"success":false}

 

Below is the PHP code that I am using for making API calls:

 

<?php

 

$auth_header = array("Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NTAzNDMyNjEsInNjb3BlcyI6Indwcm8gd251dCB3c2xlIHdzZXQgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiIzWDY5UkwiLCJhdWQiOiIyMjlYQ0QiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0NTAzMzk2NjF9.69r0RRrQ8TFFvRH-EA1TJtKTExlFPh7f5u3Et125oVk",
);


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,$auth_header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($curl);
curl_close($curl);

 

$results = json_decode($result, true);
echo $result;

 

?>

 

Can you please look into this issue and let me know where am I going wrong in making the API call?  It appears that this issue is happening with all the time series related APIs.

 

Thanks & Regards,

Srinivas

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Hi jmitchell,

 

Thank you very much for your time and valuable suggestions.  With the help of your suggestions I was able to solve the issue that I was facing!

 

As you had suggested, I did a var_dump of curl using the PHP function var_dump(curl_getinfo($curl)).  I then noticed the following settings in the request_header:

 

'request_header' => string 'POST /1/user/-/activities/steps/date/today/1d.json HTTP/1.1
Host: api.fitbit.com
Accept: */*
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NTA3MDUyNzEsInNjb3BlcyI6Indwcm8gd251dCB3c2V0IHdzbGUgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiIzWDY5UkwiLCJhdWQiOiIyMjlYQ0QiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0NTA3MDE2NzF9.AOxLlubesLJ2L9BRxtcWq428RhIpd3_4LbLBKnauzbM

' (length=382)

 

After analysing the request_header and experimenting the API call in apigee console I realised that my code was doing a POST to the endpoint and not GET.  That is where the problem was.  

 

I then changed the following line in my curl execution code which I had posted earlier:

 

old line: curl_setopt($curl, CURLOPT_POST, true);

modified line: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request_type);

 

$request_type in the above line will be either GET or POST as required by the calls various respective endpoints.

 

However, call to certain endpoint such as the get profile end point will work regardless of whatever type of execution (POST or GET).  But most of the data retrieving end points work with GET only.

 

So, bottom line of the solution to the problem is try executing the end point with POST.  If it does not work try with GET.

 

Thanks a ton again for your valuable suggestions.  They helped me a lot to get into the root of this issue and solve it permanently.

 

Thanks & Regards,

Srinivas

 

 

View best answer in original post

Best Answer
0 Votes
4 REPLIES 4

You can browse through the available endpoints using the API console here:

https://apigee.com/me3/embed/console/fitbit

 

It provides oauth1 authentication against a users account. It's also useful to see what endpoints are available and to debug any api errors that you may have.


API: https://api.fitbit.com/1/user/-/activities/steps/date/today/7d.json

 

{"errors":[{"errorType":"not_found","fieldName":"n/a","message":"The API you are requesting could not be found."}],"success":false}

 


Now I just tested that URL against my own account and it worked, and will work regardless of oauth1 or oauth2 authentication. The only reason it wouldn't work is because of an error with the generated URL.

 

Could you paste the full url that you're passing to curl (the curl($url) component). Also take note that curl is executed by php as a command line interface, so you need to encode any special/breaking characters in the url just as you would if you executed it from command line yourself. I've run in to this issue myself a multitude of times using curl through php. I've stopped using curl and started using guzzle and other libraries to process http requests since they handle the encoding for you.

 

If you want to look at libraries, you can use @SunsetRunner's library on GitHub, or my library on GitHub. Either work and provide at least the functionality to connect to oauth2, but don't provide request endpoints as class methods. DanC has another library for that and in due time I'll add up another repository to do the same thing too.

Best Answer
0 Votes

Hi jmitchell,

 

Thank you so much for the reply.  

 

The url that I am passing to curl in curl($url) is:

https://api.fitbit.com/1/user/-/activities/steps/date/today/7d.json

 

As you can see there are no special characters like breaking space etc in the URL.  Are you saying that I need to encode the complete URL? Do you have any sample PHP code which encodes the URL as required to be accepted by Fitbit?

 

Regarding using other libraries, I will definitely take a look at them.

 

Again, Thanks very much for the help.

 

Thanks & Regards,

Srinivas

Best Answer
0 Votes

1. Try to capture exact request details, you can do this with cURL in php by capturing the request headers.

2. Dump the raw request uri and then attempt to request it using cURL on CLI (eg. curl http://blah.com -X post etc.)

 

You can also use proxy tools to capture requests and that will let you look at the request uri as well as the request headers.

 

In any case, you need the raw request uri, since this is the one that's giving you the problems. The best way would be to var_dump it into the error log, or var_dump and then exit the script straight away.

Best Answer
0 Votes

Hi jmitchell,

 

Thank you very much for your time and valuable suggestions.  With the help of your suggestions I was able to solve the issue that I was facing!

 

As you had suggested, I did a var_dump of curl using the PHP function var_dump(curl_getinfo($curl)).  I then noticed the following settings in the request_header:

 

'request_header' => string 'POST /1/user/-/activities/steps/date/today/1d.json HTTP/1.1
Host: api.fitbit.com
Accept: */*
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NTA3MDUyNzEsInNjb3BlcyI6Indwcm8gd251dCB3c2V0IHdzbGUgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiIzWDY5UkwiLCJhdWQiOiIyMjlYQ0QiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0NTA3MDE2NzF9.AOxLlubesLJ2L9BRxtcWq428RhIpd3_4LbLBKnauzbM

' (length=382)

 

After analysing the request_header and experimenting the API call in apigee console I realised that my code was doing a POST to the endpoint and not GET.  That is where the problem was.  

 

I then changed the following line in my curl execution code which I had posted earlier:

 

old line: curl_setopt($curl, CURLOPT_POST, true);

modified line: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request_type);

 

$request_type in the above line will be either GET or POST as required by the calls various respective endpoints.

 

However, call to certain endpoint such as the get profile end point will work regardless of whatever type of execution (POST or GET).  But most of the data retrieving end points work with GET only.

 

So, bottom line of the solution to the problem is try executing the end point with POST.  If it does not work try with GET.

 

Thanks a ton again for your valuable suggestions.  They helped me a lot to get into the root of this issue and solve it permanently.

 

Thanks & Regards,

Srinivas

 

 

Best Answer
0 Votes