Setting Up the Credentials
Follow step-by-step instructions to set up a project and generate the required credentials.
We need to complete a few steps before we can start using YouTube Data API. Follow the steps below to set up your working environment.
Set up a project
We need a Google account to access the Google API Console to generate an API key and register your app.
First, create or select a project in the API Console.
Next, create a project by following the steps below:
- For the “Project name” field, enter a name that best describes your project.
- For the “Location” field, leave it as “No organization”.
- Click the “Create” button.
These steps are illustrated below.
Once we’ve created the project, we’ll be redirected to the project dashboard.
Enable the API for the project
Enable the API by following the steps below:
- Go to the “Library panel” and search for “YouTube Data API v3.”
- In the search results, select “YouTube Data API v3.”
- Click the “Enable” button to ensure that the API is enabled for your project.
After we enable the API, we’ll be redirected to the API overview page.
Now that we have enabled the API for our project, we’ll create an API key in the next step.
Create an API key
We’ll use the API key to make API requests that don’t require user authorization. For example, we don’t need user authorization to retrieve information about a public YouTube video or public channel.
To create an API key, follow the steps below:
- Go to the “Credentials” panel and select “Credentials” from the sidebar.
- Click the “CREATE CREDENTIALS” button.
- Select the “API key” option from the dropdown menu.
A pop-up will appear containing the value of the API key. These steps are illustrated below.
Save the API key
Now that the API key for the project has been generated, copy it to use it in the course. Let’s save the API key to use it throughout the course. To do so, follow these steps:
- Click the “Edit” button in the following widget.
- Paste the API key in the
API_KEY
field. - Click the “Save” button.
As soon as the value is saved, it will be replaced in the code below. We can print this value by clicking on the “Run” button.
api_key = "{{API_KEY}}"print('API_KEY: '+ api_key)
Configure consent screen
We’ll use a client ID to make API requests that require user authorization. For example, we’ll need user authorization to retrieve information for a personal YouTube video or channel.
If you’re creating a client ID for the first time, you might be asked to configure your consent screen. To do so, click the “CONFIGURE CONSENT SCREEN” button as shown below.
Next, we’ll be asked to choose the user type for configuring your app. Choose “External” and click the “CREATE” button.
A few steps are required to register an app. They are discussed in detail below.
OAuth consent screen
Fill in the “App name” and “User support email” under the “App information” section. On the same page, under the “Developer contact information” section, add an email address of your choice to get notified about changes to your project.
Finally, click the “SAVE AND CONTINUE” button to advance to the next section.
Scopes
Scopes are the permissions that you request from the users to authorize for your app. We won’t add any scopes for our app, so we can just click “SAVE AND CONTINUE” to continue with the setup.
Test users
We’ll use our app in the test state. Let’s add test users so that they can access the app.
Follow the steps below to add a test user:
- Click “ADD USERS” to add a test user.
- In the window that appears, enter the email address of the test user and click the “ADD” button.
- Close the window to verify that the email has been added in the “User information” table. Finally, click “SAVE AND CONTINUE” to move forward.
Summary
In the “Summary” section, we can verify the details of the app. These details are the ones you set in the previous sections. After confirming the details, click the “BACK TO DASHBOARD” button to configure the consent screen.
Create an OAuth 2.0 client ID
To create an OAuth client ID, follow the steps below:
- Go to the “Credentials panel” and select “Credentials” from the sidebar.
- Click the “CREATE CREDENTIALS” button.
- Select the “OAuth client ID” option from the menu.
- You’ll be asked to select the “Application type” and set the “Name” of your OAuth 2.0 client. Choose “Desktop app”, set an appropriate name for the client, and click the “CREATE” button.
A pop-up will appear that contains the client ID and secret key. These steps are illustrated below.
Save the client secret
Now that you’ve created an OAuth client ID for your project, a JSON file named client_secret_<CLIENTID>.json
, where <CLIENTID>
is the client ID, is available for download. The file contains your OAuth 2.0 credentials, so download it to your local device.
We’ll need the contents of the downloaded JSON file later in the course when we make authorized API requests. Once we’ve downloaded the client_secret_<CLIENTID>.json
file locally, we’ll open it and copy the complete JSON object in the file. Follow the steps below to save it on Educative’s platform.
- Click the “Edit” button in the following widget.
- Paste the complete copied JSON object in the
CLIENT_SECRET
field. - Click the “Save” button.
import jsonclient_secret = {{CLIENT_SECRET}}# The client secret will be converted into a JSON object using the "json" libraryjson_object = json.dumps(client_secret, indent = 4)print("Client Secret: \n" + json_object)