是否可以禁用 google-API 授权?

Is it possible to disable google-API authorization?

我正在编写一个简单的 python 脚本,它使用 google-API 获取 YouTube 视频持续时间。 这是我写的代码:

# imports
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import aniso8601

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
# 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"

# File name which has the credentials(API/OAuth)
client_secrets_file = "MY_FILE_NAME.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.videos().list(
    part="contentDetails",
    id="CbxQWAFv7sA"

)
response = request.execute()
duration_in_iso_8601 = response['items'][0]['contentDetails']['duration']
duration_in_seconds = aniso8601.parse_duration(duration_in_iso_8601).seconds
print(duration_in_seconds)

现在,除了每次我 运行 脚本都要求我输入授权码之外,一切正常:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=MY_ID.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&state=i4RuG2QRXmef9t5MQUoOOqSXGhvMM5&prompt=consent&access_type=offline
Enter the authorization code: 

我的目标是最终脚本将在不同的设备上自动执行,因此上述内容会中断流程。我想知道,有什么办法可以禁用此提示吗?如果没有,是否有任何其他方法可以实现此授权不会干扰流程? 非常感谢!

Actually, I don't want any authentication, if possible using this API.

您需要了解您正在访问私人用户数据。为了访问私人用户数据,您需要获得拥有该数据的用户的许可。为了获得许可或同意,我们使用 Oauth2。 YouTube 没有其他选项 API 您需要征得同意。

public数据

您正在访问的方法video.list is actually based upon public data not private user data. Which means you should be using an API key to access it. How to create a YouTube API key

youtube = build('youtube', 'v3', developerKey='api_key')

凭证存储Oauth

如果您确实需要访问私人用户数据。 Google analytics API 有一个很好的例子 python。它展示了如何将您的凭据存储在 .dat 文件中。此代码是单个用户,但您可以更改它,以便每个用户的凭据将存储在不同的文件中。

我尽力为 YouTube 稍作改动 API。如果您有任何问题,请告诉我。

SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']
CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.

def initialize_youtube():
  """Initializes the youtube service object.

  Returns:
    analytics an authorized youtube service object.
  """
  # Parse command-line arguments.
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args([])

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(
      CLIENT_SECRETS_PATH, scope=SCOPES,
      message=tools.message_if_missing(CLIENT_SECRETS_PATH))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage('youtube.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=httplib2.Http())

  # Build the service object.
  youtube = build('youtube', 'v3', http=http)

  return youtube