1

I use this sample of code I found on the YouTube dev help site to post comment on a video

# -*- coding: utf-8 -*-

# Sample Python code for youtube.commentThreads.insert
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

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

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

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 = "post.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 = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.commentThreads().insert(
        part="snippet",
        body={
          "snippet": {
            "videoId": "VIDEO_ID",
            "topLevelComment": {
              "snippet": {
                "textOriginal": "This is the start of a comment thread."
              }
            }
          }
        }
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

the comment is working but EACH time it ask me to authorize the application again I don't know why, can you help me ? the message I get is :

"Please visit this URL to authorize this application: URL_LINK

Enter the authorization code:"

Beso
  • 1,176
  • 5
  • 12
  • 26

1 Answers1

0

It is because you are not utilizing the refresh token in your code. This refresh token allows you to get an access token without logging in again.

The link below should help you utilize it.

Google API: getting Credentials from refresh token with oauth2client.client

goblin
  • 1,513
  • 13
  • 13