0

I'm trying to create a playlist:

#Imports
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import json

#Load secrets
with open('client_secret_XXXX.apps.googleusercontent.com.json', 'r') as f:
    info = json.load(f)

#Credentials
creds = Credentials.from_authorized_user_info(info=info, scopes=["https://www.googleapis.com/auth/youtube"])

#API
youtube = build('youtube', 'v3', credentials=creds)

#Create playlist
youtube.playlists().insert(
    part='snippet',
    body={
        'snippet': {
            'title': 'MyNewPlaylistTest',
            'privacyStatus': 'private'
        }
    }
).execute()

The error that appears:

Traceback (most recent call last):   File "x.py", line 9, in <module>
   creds = Credentials.from_authorized_user_info(info=info, scopes=["https://www.googleapis.com/auth/youtube"])
File "y.py", line 352, in from_authorized_user_info
    **raise ValueError( ValueError: Authorized user info was not in the expected format, missing fields client_secret, client_id, refresh_token**.

I'm using the json credentials from the Client ID in OAuth2.0 in my google cloud.

--

Content of the client secret json:

{"installed":{
     "client_id":"XXXX.apps.googleusercontent.com",
     "project_id":"YYYYYYYY-XXXX",
     "auth_uri":"https://accounts.google.com/o/oauth2/auth",
     "token_uri":"https://oauth2.googleapis.com/token",
     "auth_provider_x509_cert_url":"XXXXX/oauth2/v1/certs",
     "client_secret":"XXXXXXXX",
     "redirect_uris":["http://XXXXX"]
     }
 }
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Yen
  • 59
  • 5
  • `client_secret_XXXX.apps.googleusercontent.com.json` Can you show us the contents of this file? Of course you should replace any actual secret values with XXX's. – John Gordon Jan 18 '23 at 20:28
  • It looks like that function expects `client_secret`, `client_id`, and `refresh_token` to be top-level keys in the json data, but instead you have them as sub-keys underneath the key `installed`. Are you sure that's right? How exactly was that file created? – John Gordon Jan 18 '23 at 21:00
  • I also tried doing info=info['installed'] but to not avail. The file is created when you request a OAuth 2.0 Client ID from console.cloud.google in your project. – Yen Jan 18 '23 at 21:06
  • `info=info['installed']` is better, although it is still missing a `refresh_token` key. – John Gordon Jan 18 '23 at 21:08
  • My (vague) impression of refresh tokens is that they are short-lived, so it kind of makes sense that there isn't one in the file. Maybe you're supposed to request a new refresh token and merge it into the client dict, then call the function? – John Gordon Jan 18 '23 at 21:29
  • Thanks for pointing that out. Looking at that issue more specifically now and found this (will try to see their solutions) https://stackoverflow.com/questions/69747758/refresh-token-is-missing-in-google-oauth2-net-httppost-request-for-access-token – Yen Jan 18 '23 at 21:33

0 Answers0