I
I
Ilkhomjon Matazimov2021-03-22 16:59:40
YouTube
Ilkhomjon Matazimov, 2021-03-22 16:59:40

How to save an authorization session in YouTube?

Good day to all.
Please help me. I have a script that writes comments under my YouTube videos.
But on every request, YouTube asks me to log in.

Question: How can I make sure that I log in once and that's it, so that I don't have to go through authorization again?

The code:

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

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


def main():
    channel = input('Channel id: ')
    video = input('Video id: ')
    commenttext = input('Comment text: ')
    # 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)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.commentThreads().insert(
        part="snippet",
        body={
            "snippet": {
                "channelId": channel,
                "videoId": video,
                "topLevelComment": {
                    "snippet": {
                        "textOriginal": commenttext
                    }
                }
            }
        }
    )
    response = request.execute()

    print(response)


if __name__ == "__main__":
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-03-22
@sergiks

Save credentials and reuse what you get.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question