1
votes

I have been working on this problem for a while,

I am using youtube API to list out all playlist items in a channel, I have read this below: Youtube Data API - How to avoid Google OAuth redirect URL authorization

But I still need to open the browser and auth my program to access my own youtube account every time I run it.

According to this page: https://developers.google.com/youtube/v3/getting-started

4. If your application will use any API methods that require user authorization, 
read the authentication guide to learn how to implement OAuth 2.0 authorization.

All I need is listing all available video in a channel.

Code examples provided by google:

import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from google_auth_oauthlib.flow import Flow

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

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

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "CREDFILE.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.playlistItems().list(
        part="snippet",
        maxResults=30,
        playlistId="PLAYLISTID"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

I have put my credential.json files as the code specified, but I still have to auth myself, then paste the auth code in the command line. I am confused about the use of supplying credential.json files here.

It seems like credentials = flow.run_console() caused this, could it be possible having other method to get the credential?

2
Just a quick note: for listing all public videos of a given channel there's no need for one to use OAuth authorization flow at all! It suffices to have a valid -- freely obtainable from Google console -- API application key and to pass that to PlaylistItems.list endpoint.stvar

2 Answers

0
votes

The way Oauth2 works is that it requires user consent in order to grant access the to the users data. That consent is granted is though the browser window requesting the user grant your application access.

You are always going to have to grant consent at least once, there is no way around that with the YouTube API.

The other question you are looking at (Youtube Data API - How to avoid Google OAuth redirect URL authorization ) only explains that once the refresh token is stored once that you wont need to load it again. Which is possible with the Google api Java client library because user credential storage is built into that library.

You would have to code something similar in the python client library which i dont think supports that.

0
votes

You only need user authentication to get details or make modifications for your own playlist. From your question and code it looks like you just want to get the list of videos from any playlist.

You don't need user authentication to get a public list of items in a playlist. You only need to make an HTTP call to API playlistItems endpoint with an API key.

From page https://developers.google.com/youtube/v3/docs

Every request must either specify an API key (with the key parameter) or provide an OAuth 2.0 token. Your API key is available in the Developer Console's API Access pane for your project.

Your HTTP call will look like this:

https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=PLeskMkEaHJYc_B6QFIaEwps1LXUpXx0_s&maxResults=50&key=[API_KEY]