And here’s where I got stuck myself, and how I eventually managed to resolve it:
As per the Fitbit docs, the final step of implementing the subscription API is to create a subscription. That is, one must send a POST request of the following form: /1/user/[user-id]/[collection-path]/apiSubscriptions/[subscription-id].json
.
For example, to subscribe to my own sleep data, I’d use this URL: https://api.fitbit.com//1/user/-/sleep/apiSubscriptions/arbitrary_string.json
.
These are the required headers:
headers = {
'accept': 'application/json',
'content-length': "0",
"X-Fitbit-Subscriber-Id": "your_subscriber_id_configured_at_dev.fitbit.com/apps",
"Authorization": authorization
}
But what is the correct authorization? Here’s where I got stuck for a long time. I initially used Pipedream’s Fitbit authorization, i.e. the following:
token = f'{pd.inputs["fitbit"]["$auth"]["oauth_access_token"]}'
authorization = f'Bearer {token}'
headers = {"Authorization": authorization}
When using this authorization to create a subscription, I got a cryptic error response from Fitbit, indicating that my given subscriber ID didn’t exist, even though it clearly did. I didn’t understand this for a long while, but eventually realized that Pipedream’s authorization above presumably belongs to their own Fitbit app configured at dev.fitbit.com/apps, which I can’t access, and thus I can’t create subscribers for that app.
So instead, one has to manually authorize one’s own Fitbit app via Fitbit’s interactive OAuth 2.0 Tutorial. (This tutorial looks rather long but essentially just consists of pasting a bunch of values and clicking a few buttons, until one finally receives an OAuth2 Access Token, which is valid for 8 hours.)
Once that’s done, one can straightforwardly create a subscriber by using a one-of Pipedream workflow which makes the following HTTP request (here as Python code):
import requests
def handler(pd: "pipedream"):
token = "your_temporary_OAuth2_access_token_goes_here"
authorization = f'Bearer {token}'
headers = {
'accept': 'application/json',
'content-length': "0",
"X-Fitbit-Subscriber-Id": "your_subscriber_id_configured_at_dev.fitbit.com/apps"
"Authorization": authorization
}
// Example Requests URL to subscribe to one's own "sleep" data.
r = requests.post('https://api.fitbit.com/1/user/-/sleep/apiSubscriptions/pipedream-sleep.json', headers=headers)
print(r.status_code, r.reason)
return r.json()