pythonyoutube-analytics-apideepnote

token issues youtube analytics api python cloud


I get the next code used to pull youtube information form the youtube analytics api (not youtube data API V3)

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="video",
        endDate="2014-06-30",
        ids="channel==MINE",
        maxResults=10,
        metrics="estimatedMinutesWatched,views,likes,subscribersGained",
        sort="-estimatedMinutesWatched",
        startDate="2014-05-01"
    )
    response = request.execute()

    print(response)

I get the client secret and the code runs fine, but i'm running the code on cloud(deepnote specifically) so at some point the code requires to enter manually the token, is there a way to avoid this, or to pull the token in someway? because I don´t see possible to retrive the token when running on cloud this code.

Thanks in advance.


Solution

  • The OAuth flow is designed to be interactive. It is usually used in cases where you (an app/web developer) want a user to click a link to log in with their own account and on their own. This usually requires a redirect, or a popup window, or an input field (like in Colab or Jupyter) where they do this kind of interaction.

    The right way to do this kind of automatic requests is via a different kind of API, or using a different kind of authentication. In many cases of GCP APIs you'd use service account, but this particular API does not support service account authentication.

    If you really want to use OAuth client secret flow, you can do it in Deepnote, when you run the flow.run_console() command, you'll get an input where you have to enter your approved token by your (user) google account. However, for this to work, you have to create a Desktop app client secret, and allow your user to access the testing mode, and click through the OAuth warnings. Or you have to publish the OAuth app, which requires approval from Google.

    Here, I published an example Deepnote notebook which shows this is possible: https://deepnote.com/@jz/YouTube-Analytics-API-vuA9wCgGRKiGN9Mjaoi54Q

    But this requires the manual interaction which you probably don't want. In other GCP APIs you could use your service account like this:

    credentials = service_account.Credentials.from_service_account_file(service_account_file, scopes=scopes)
    
    

    So if you really need to be automated, you need to use a different YouTube API.