Shahnawaz Irfan
Shahnawaz Irfan

Reputation: 189

ERROR 403 Forbidden YouTube Analytics API

I'm trying to getting reports using YouTube Analytics API but 403 forbidden stucks me. I search a lot on google but no relevant solution found. I 'm trying a code from its official docs with google oAuth 2.0 .

import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtubeAnalytics"
    api_version = "v2"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube_analytics = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube_analytics.reports().query(
        dimensions="day",
        endDate="2018-12-01",
        ids="channel==UCzxqTj9GZVeSy_Er9VPvKEQ",
        metrics="views",
        startDate="2018-11-29"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()
Error

{
  "error": {
    "code": 403,
    "message": "Forbidden",
    "errors": [
      {
        "message": "Forbidden",
        "domain": "global",
        "reason": "forbidden"
      }
    ]
  }
}

Upvotes: 1

Views: 1175

Answers (2)

Johanna
Johanna

Reputation: 660

After making sure you have enabled the YouTube Analytics API for use in the project...

If you are still getting the error, it might be because:

  1. You didn't add yourself as a test user in the Google Cloud Project --> OAuth consent screen
  2. You are trying to access your brand account analytics, and not your own channel analitycs. In this case, this answer here solved the problem for me.

Upvotes: 0

Boolean
Boolean

Reputation: 163

That HTTP 403 response may indicate that some part of the google account you are trying to use has not been authorized/configured properly. You may wish to go to your Google cloud console and check that you have:

Enabled the YouTube Analytics API for use in the project you are wanting to use it in. Follow the Authorization flow for the API. It should be beared-in-mind that there is currently no support for accessing channel data through service accounts (but analytical data can be accessed on behalf of another user.)

Extract from the API (June 2021):

  • The YouTube Analytics API does not support the service account flow.
  • The YouTube Reporting API only supports the service account flow for YouTube content owners that own and manage multiple YouTube channels. Specifically, content owners can use service accounts in API requests that set a value for the onBehalfOfContentOwner request parameter.

Upvotes: 1

Related Questions