bestgu
bestgu

Reputation: 49

I keep getting authentication errors with the Google API

I've been trying to make a script that update the YouTube title with the video views, like the Tom Scott video. But I keep getting this error with the authentication.

I get the code to authenticate it but it just says I don't have enough permission.

<HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=%28%27snippet%2Cstatus%2Clocalizations%27%2C%29&alt=json returned "'('snippet'". Details: "[{'message': "'('snippet'", 'domain': 'youtube.part', 'reason': 'unknownPart', 'location': 'part', 'locationType': 'parameter'}]"> File "C:\Users\erik\Documents\PYTHON CODE\view_changer\example.py", line 49, in main response_update = request_update.execute().

Here's my code:

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

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


video_1_part_view = 'statistics'
video_1_id_view = 'hVuoPgZJ3Yw'
video_1_part_update= "snippet,status,localizations",
video_1_part_body_update= { "id": "1y94SadSsMU",
                            "localizations": {"es": {    "title": "no hay nada a ver aqui",
                                                         "description": "Esta descripcion es en español."
                                                        }
                                                }
                            }

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 = (r"C:\Users\erik\Documents\PYTHON CODE\view_changer\client_secret_379587650859-bbl63je7sh6kva19l33auonkledt09a9.apps.googleusercontent.com.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)

    #check views
    request_view= youtube.videos().list( part = video_1_part_view, id = video_1_id_view )
    respons_view = request_view.execute()
    views = respons_view['items'][0]['statistics']['viewCount']
    print (views)
   
    #update video
    request_update = youtube.videos().update(part= video_1_part_update, body = video_1_part_body_update)
    response_update = request_update.execute()
    print(response_update)

main()
# if __name__ == "__main__":
#     main()

If the problem is not in the code I might have an other idea of what it might be. When I was first setting up the OAuth, I didn't know I needed to pick what the user would be giving permission to (I know rookie mistake(: ), but since I updated it, when I ask for permission after going to the authentication link the code gives me back, it doesn't ask permission for all the things I told it to do.

Upvotes: 0

Views: 96

Answers (1)

stvar
stvar

Reputation: 6985

According to the official documentation, the request parameter part of the Videos.list API endpoint is defined as:

part (string)

The part parameter specifies a comma-separated list of one or more video resource properties that the API response will include.

[...]

But you're having that request parameter set as:

('snippet,status,localizations',).

That's because (though your code above doesn't show it) you have the variable video_1_part_view defined as:

video_1_part_view = "snippet,status,localizations",

(Do notice the trailing comma). That makes video_1_part_view a Python tuple, which is not what you need to send to the API endpoint.

The same is true for your variable video_1_part_update.

Just remove the trailing commas and things will work OK.

Upvotes: 1

Related Questions