12-29-2023 06:08
12-29-2023 06:08
I followed the instructions for Implementing the Subscription API. I have created a web service endpoint, configured a subscriber, and verified the subscriber, but I cannot get the final step of Creating a Subscription to work.
I always receive the following error: 400 Bad Request. errorType: validation. fieldName: subscriberId. message: Invalid parameter subscriberId: 1234
Screenshot of how my app is configured, incl. that it indeed has the subscriber ID "1234".
Here's my Python code, using the automation platform Pipedream:
import requests
def handler(pd: "pipedream"):
token = f'{pd.inputs["fitbit"]["$auth"]["oauth_access_token"]}'
authorization = f'Bearer {token}'
headers = {
'accept': 'application/json',
'content-length': "0",
"X-Fitbit-Subscriber-Id": "1234",
"Authorization": authorization
}
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()
What's the problem here?
Answered! Go to the Best Answer.
01-15-2024 03:47
01-15-2024 03:47
Hey there, thanks for the reply. I found the problem. When using the automation service Pipedream (and presumably this also applies to similar services like Zapier or Make), one grants an OAuth2 authorization to only that service, i.e. presumably they have their own Fitbit app configured at dev.fitbit.com/apps. In any case, by using the code I mentioned in the OP, I was not using an authorization for my own app configured at dev.fitbit.com/apps, but rather for another, and so the subscriber ID I provided indeed didn't exist. Once I followed the OAuth2 tutorial from dev.fitbit.com/apps for my own app, received an OAuth2 access token, and used that token in my code, the rest of the setup worked fine.
Feedback from this experience, for the Fitbit API: I wish Fitbit's error message had been clearer here. It was "400 Bad Request. errorType: validation. fieldName: subscriberId. message: Invalid parameter subscriberId: 1234". But if this error message had also included the name and/or client ID of the Fitbit app I was authenticated with (e.g. "message: Invalid parameter subscriberId: 1234 for client ID: XYZ"), I would've caught this error instantly, instead of only after several hours of troubleshooting.
01-02-2024 13:40
01-02-2024 13:40
Hi @MondSemmel
The information you provided looks correct to me. Let me send you an email to request some additional information on your specific use case.
01-09-2024 23:43
01-09-2024 23:43
@MondSemmel Are you updating the content-length header with the actual payload size when sending the request? I'm thinking the request might be failing because of this.
01-15-2024 03:47
01-15-2024 03:47
Hey there, thanks for the reply. I found the problem. When using the automation service Pipedream (and presumably this also applies to similar services like Zapier or Make), one grants an OAuth2 authorization to only that service, i.e. presumably they have their own Fitbit app configured at dev.fitbit.com/apps. In any case, by using the code I mentioned in the OP, I was not using an authorization for my own app configured at dev.fitbit.com/apps, but rather for another, and so the subscriber ID I provided indeed didn't exist. Once I followed the OAuth2 tutorial from dev.fitbit.com/apps for my own app, received an OAuth2 access token, and used that token in my code, the rest of the setup worked fine.
Feedback from this experience, for the Fitbit API: I wish Fitbit's error message had been clearer here. It was "400 Bad Request. errorType: validation. fieldName: subscriberId. message: Invalid parameter subscriberId: 1234". But if this error message had also included the name and/or client ID of the Fitbit app I was authenticated with (e.g. "message: Invalid parameter subscriberId: 1234 for client ID: XYZ"), I would've caught this error instantly, instead of only after several hours of troubleshooting.