Home > Enterprise >  How to fix 403 error when creating GoogleFit datasource
How to fix 403 error when creating GoogleFit datasource

Time:09-27

I am using GoogleFit Rest API to create a nutrition data source so that I can subsequently store nutrition information. I have done this successfully on Android via the GoogleFit SDK, but because no such SDK is available on iOS, I resorted to using the REST APIs via the link above. When trying to create the data source I am returned the following error:

{
    "error": {
        "code": 403,
        "message": "The caller does not have permission",
        "errors": [
            {
                "message": "The caller does not have permission",
                "domain": "global",
                "reason": "forbidden"
            }
        ],
        "status": "PERMISSION_DENIED"
    }
} 

and the POST request I am using is:

Endpoint: https://www.googleapis.com/fitness/v1/users/me/dataSources

Body:

{
    "application": {
        "detailsUrl": "https://strongfoundation.dev",
        "name": "Strong Foundation",
        "packageName": "com.coding.casa.Strong.Foundation",
        "version": "1.0.1"
    },
    "dataStreamId": "raw:com.google.nutrition:com.coding.casa.Strong.Foundation:Apple:iPad Air (3rd generation):****",
    "dataType": {
        "name": "com.google.nutrition"
    },
    "device": {
        "manufacturer": "Apple",
        "model": "iPad Air (3rd generation)",
        "type": "phone",
        "uid": "*****",
        "version": "15.7"
    },
    "name": "strong-foundation-food",
    "type": "raw"
}

Has anyone seen this error and was able to resolve it? I have tried adding the API Key to the request, but that did not change the error, and I do have the Fitness API enabled on my project.

EDIT:

I am also supplying a Bearer token with the following authorized scopes

    "https://www.googleapis.com/auth/fitness.activity.read",
    "https://www.googleapis.com/auth/fitness.body.write",
    "https://www.googleapis.com/auth/fitness.body.read",
    "https://www.googleapis.com/auth/fitness.nutrition.read",
    "https://www.googleapis.com/auth/fitness.nutrition.write",

CodePudding user response:

You don't include the others details of the REST method call.

Importantly, HTTP 403 is Forbidden suggesting that you didn't include an Authorization header in your request with value Bearer {TOKEN} where {TOKEN} is an access token for an identity that has suitable scopes to access the API. See authorization

Although Swift is not one of Google's officially supported languages, Google has a Swift REST Client Generator for Google APIs and you should be able to use it to generate a Google client library for Swift for Google Fit.

CodePudding user response:

Did some more playing around with the FitnessAPI (specifically creating data sources) and found the issues was with my request payload.

The payload that worked for me is as follows:

{
    "application": {
        "detailsUrl": "https://strongfoundation.dev",
        "name": "Strong Foundation",
        "version": "1.0.1"
    },
    "dataType": {
        "name": "com.google.nutrition"
    },
    "device": {
        "manufacturer": "Apple",
        "model": "iPad Air (3rd generation)",
        "type": "phone",
        "uid": "****",
        "version": "15.7"
    },
    "name": "strong-foundation-food",
    "type": "raw"
}

The deltas from the original one are removed the following properties:

  • application.packageName
  • dataStreamId
  • Related