Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - The request is missing a Valid API Key with service account authorization

I'm trying to pull youtube comments into a dataframe. I am able to pull details of the youtube video but a receive a 403, "The request is missing a valid API key." error when I try to receive the commentThreads resource.

def get_service(api_name, api_version, scopes, key_file_location):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
            key_file_location, scopes=scopes)

    # Build the service object.
    service = build(api_name, api_version, credentials=credentials)

    return service

# Authenticate and construct service.
service = get_service(
        api_name='youtube',
        api_version='v3',
        scopes='https://www.googleapis.com/auth/youtube.force-ssl',
        key_file_location='key_file.json')

The key_file.json is a service account key credential. This is there where I get the error:

    response = service.commentThreads().list(
                      part = 'snippit',
                      videoId = video_id,
                      maxResults = 100,
                      order = 'relevance',
                      textFormat = 'plainText',
                      pageToken = nextPage_token).execute()

I'm not sure why I am receiving a forbidden error here as when I do the following function, it works fine:

query_results = service.search().list(part = 'snippet', q = query,
                      order = 'relevance',
                      type = 'video',
                      relevanceLanguage = 'en',
                      safeSearch = 'moderate',).execute()
question from:https://stackoverflow.com/questions/65877328/the-request-is-missing-a-valid-api-key-with-service-account-authorization

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

credentials = ServiceAccountCredentials.from_json_keyfile_name( key_file_location, scopes=scopes)

You appear to be trying to use service account authentication with the YouTube API. The YouTube API does not support service accounts you will need to use Oauth2 to authenticate your users.

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

  Returns:
    youtube 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...