'YT Data API throws Error 403 for just one video
I was using the YouTube Data API v3 to put the current view count in my video title just like in this video ( https://www.youtube.com/watch?v=BxV14h0kFs0 ) and it works!
I set it up in pythonanywhere for this video ID: 7vSw7XRivWY, worked like a charm, still running. Then I tried to do the same thing for another video (ID: fDh-qcbIxtU ) and now it throws
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/youtube/v3/videos?part=snippet&alt=json returned "The request metadata is invalid.">
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
this error. I'm using the exact same code I used for the other ID, all I did was changing my variable for the video ID to the other one.
I've no clue why this happens. I retried it multiple times, I tried it with different API scopes, I tried it with another API client key - same result, not working.
In my python code I implemented a method change_title that takes the apiclient and the video ID as parameters then first fetches the view count & snippet part ( which contains the title ), edits the title section of the snippet, and then updates the video accordingly.
As already stated, it works for any other of my videos but not for this one (ID: fDh-qcbIxtU). I'm 100% sure I put in the correct ID and that I didn't change anything else.
Here's the whole file that is being executed:
# -*- 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
import time
scopes = ['https://www.googleapis.com/auth/youtube.force-ssl']
def like_video(youtube, videoID):
youtube.videos().rate(
id=videoID,
rating='like'
).execute()
def change_title(youtube, videoID):
video_list_response_statistics = youtube.videos().list(
id=videoID,
part="statistics"
).execute()
videos_list_statistics = video_list_response_statistics['items'][0]['statistics']
views = videos_list_statistics['viewCount']
print("Views: " + str(views))
snippetPart = youtube.videos().list(
id=videoID,
part="snippet"
).execute()
# Since the request specified a video ID, the response only contains one
# video resource. This code extracts the snippet from that resource.
videos_list_snippet = snippetPart['items'][0]['snippet']
print(videos_list_snippet)
videos_list_snippet['title'] += " [" + str(views) + " Views]"
videos_update_response = youtube.videos().update(
part='snippet',
body=dict(
id=videoID,
snippet=videos_list_snippet
)).execute()
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "credentials.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)
i = 1
videoID = "fDh-qcbIxtU"
while True:
print("Update No" + str(i))
response = change_title(youtube, videoID)
print(response)
i += 1
time.sleep(1000)
if __name__ == "__main__":
main()
Thanks for anyone that's taking the time to try & solve my issue.
Solution 1:[1]
I was getting this error for my one particular youtube channel, and that was because it was managed by a brand account than its owner account.
It might not be helpful in your case but maybe it can help others facing this issue.
Just authorize with the right account.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Singh |