0

I am trying to build a simple upload application for personal use. The reason why I am posting is the google documentation and the stack overflow posts on this subject are rather confusing for a person new to google API's. Ive already went through the google cloud setup involving creating a secret key and a client id (client_secret.json)

My problem is that I want to be able to run through the o-auth2 authentication flow to gain access to the API without opening a browser. This is because the script will live on a headless Ubuntu server once complete.

Ive already read some posts about refresh tokens and access tokens in the follow posts

YouTube Data API v3: video upload from server without opening the browser

How to refresh an access token for the youtube-data-api v3

Ive also read some google documentation

https://developers.google.com/youtube/v3/guides/authentication#OAuth2_Refreshing_a_Token

https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps

These articles helped me understand whats going on but they are not actionable as it seems like the google documentation is fragmented/out of date.

This is my code that comes from the get started guide. I only had to change 1 function to get it to run.

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 = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
     # Was flow.run_console but that method was deprecated so I found the answer at https://stackoverflow.com/questions/75602866/google-oauth-attributeerror-installedappflow-object-has-no-attribute-run-co
    credentials = flow.run_local_server(open_browser=False)
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.channels().list(
        part="snippet,contentDetails,statistics",
        mine=True
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

The code runs but when I click the link generated in the output I get a redirect error but that's not important to overall issue of being unable to authenticate without a browser.

12
  • Part 1: Google authentication is performed in the browser. You cannot bypass that for Google OAuth. You must use a browser to fetch user OAuth tokens. For machine authentication, Google provides service accounts, which are part of Google Cloud, but I have not written code using service accounts and YouTube. Tip: Google Cloud services and Google services are different. YouTube is not part of Google Cloud. Commented Jan 8, 2024 at 22:56
  • Part 2: Some of your links talk about OAuth Refresh Tokens. Those tokens are used to refresh OAuth Access Tokens. You must use a browser to fetch the refresh token. Then you can get new access tokens without the browser. However, refresh tokens expire and are often invalidated. When that happens you must launch a new browser authentication session. In summary, Google has tightened security and will prevent you from bypassing browser authentication to access data stored in Google and in some cases will require you to complete an expensive security audit of your application. Commented Jan 8, 2024 at 22:59
  • 1
    Part 3: The YouTube Data API supports the service account flow only for YouTube content owners that own and manage multiple YouTube channels. Specifically, content owners can use service accounts to call API methods that support the onBehalfOfContentOwner request parameter. link Using service accounts does not require the browser for authentication. Commented Jan 8, 2024 at 23:36
  • This link might also be useful. Commented Jan 8, 2024 at 23:43
  • Thanks for your time and the info! It seems my best bet is to use the service account so I'm going to try and use that. Commented Jan 8, 2024 at 23:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.