youtubeyoutube-apiyoutube-analytics-api

Authorization Error when Connecting to Youtube Analytics API with Python


I have been trying to connect during several days to the YouTube Analytics API. I see that the solution in other posts is related about selecting 'Others' in the Application Type list. Apparently, this option is not available anymore.

I have created two different OAuth 2.0 clients: Web Application and Desktop. For the first one, it is displaying the following error:

Then, it is when I try to create a native application (Desktop App in this case), it is sending a new error message:

I have read the Google's OAuth 2.0 policy but I am not able to understand why it doesn't comply. It's not quite specific.

Anyone that has had the same issue? This is the sample code that I have been using:

import googleapiclient.errors
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

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 = "cliente_analytics.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="2021-04-01",
        ids="channel==MINE",
        maxResults=5,
        metrics="likes",
        startDate="2021-03-01"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

Thanks


Solution

  • The code you are using is designed for an installed application hence the

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    

    fixing invalid_request. You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure.

    Can be a bit tricking.