Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you specify your API key when making a request with the Google API python library?

I am getting started looking at using the YouTube API with python yet the examples given in the documentation seem to disagree with the quickstart guide.

The quickstart guide can be found here and in it they recommend you grab some sample code from the API documentation here that will request from the API some information for the "YouTube Developers" channel. Now in the quickstart guide, they say to replace the "YOUR_API_KEY" string with your API key but as you can see from the sample code it is not there.

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

# Sample Python code for youtube.channels.list
# 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.readonly"]

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 = "YOUR_CLIENT_SECRET_FILE.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.channels().list(
        part="snippet,contentDetails,statistics",
        id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

The "YOUR_CLIENT_SECRET_FILE" string i believe is only for requests that require user authentication, which should not be the case with this type of request.

So where are you supposed to supply the API key? Is the guide out of date?

All help is much appreciated!

like image 500
Lewis Avatar asked Aug 16 '19 21:08

Lewis


People also ask

Where do I use API key?

API keys provide project authorization By identifying the calling project, you can use API keys to associate usage information with that project. API keys allow the Extensible Service Proxy (ESP) to reject calls from projects that haven't been granted access or enabled in the API.

How to add an API key to your Google Cloud Platform Project?

Go to the Google Cloud Platform Console. Click the project drop-down and select or create the project for which you want to add an API key. Clicking on the NEW PROJECT will redirect you to the following page.

What is an API key and how do I find it?

The API key is a unique identifier that authenticates requests associated with your project for usage and billing purposes. You must have at least one API key associated with your project. Go to the APIs & Services > Credentials page.

How to start using an API with Python?

How to Start Using an API with Python. Having dealt with the nuances of working with API in Python, we can create a step-by-step guide: 1. Get an API key. An API Key is (usually) a unique string of letters and numbers. In order to start working with most APIs – you must register and get an API key.

Where can I learn more about API keys for Google Maps Platform?

To learn more about using API keys for Google Maps Platform APIs and SDKs, see the Google Maps Platform documentation. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.


Video Answer


1 Answers

  • You want to retrieve the channel list using an API key with YouTube Data API v3.
    • You have already had your API for using YouTube Data API v3.
  • You want to achieve this google-api-python-client with Python.
  • You have already been able to use YouTube Data API v3.

If my understanding is correct, how about this sample script?

Sample script:

Before you use this script, please set your API key.

from googleapiclient.discovery import build

api_key = "###"  # Please set your API key

api_service_name = "youtube"
api_version = "v3"
youtube = build(api_service_name, api_version, developerKey=api_key)
request = youtube.channels().list(
    part="snippet,contentDetails,statistics",
    id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)

Note:

  • This sample script supposes that you have already been able to use YouTube Data API v3. If the error related to API occurs, please confirm whether the API is enabled.

Reference:

  • google-api-python-client
    • Especially, I think that here will be useful for understanding how to use API key.

If I misunderstood your question and this was not the result you want, I apologize.

like image 55
Tanaike Avatar answered Sep 25 '22 01:09

Tanaike