0

I want to add messages to the live chat of my channel YouTube. I took an example from the API. Everything works, only the application requests Google OAuth 2.0 when entering each new message. How can this be avoided?How can I make sure that authorization is carried out only once?

I made the message send in a loop so as not to exit the application. But it did not help.

import os
import time
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

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

def main(text):
   
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client Получите учетные данные и создайте клиент API
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)

    credentials = flow.run_local_server()   #flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.liveChatMessages().insert(
        part="snippet",
        body={
          "snippet": {
            "liveChatId": "******",
            "type": "textMessageEvent",
            "textMessageDetails": {
              "messageText": text
            }
          }
        }
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main("Hello")
    time.sleep(10)
    main("Hello 10 sek")
DMP
  • 1
  • 2

0 Answers0