Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Forbidden Error when using Service Account to Authenticate against Youtube API

$
0
0

I'm writing a python script that scrapes Youtube URLs from various RSS Feeds and adds them to a Youtube Playlist via the Youtube API. I have been able to get authentication to work using a token, but am now moving the python script onto a Linux server from my Windows Workstation, so I need to get this working using a Service Account instead.

I've created a service account in Youtube, it has been assigned Owner status for the project, and I'm using some boiler plate code to create the credentials object using a downloaded copy of the service_account.json file containing the Service Account details:

def google_auth():    SCOPES = ['https://www.googleapis.com/auth/youtube']    service_account_info = json.load(open('service_account.json'))    creds=Credentials.from_service_account_info(        service_account_info, scopes=SCOPES)    drive_service = build('youtube', 'v3', credentials=creds)    return creds

The returned credentials object is then passed back into a subsequent API call to add videos to my Playlist.

for video_id in yt_video_ids:    if video_id in current_playlist_file:        print(f'Video "{video_id}" already in playlist, skipping.')    else:        print(f'Adding "{video_id}" to Playlist.')        google_api_stuff.add_video(playlist_id, video_id, credentials)
def add_video(playlist_id, video_id, credentials):    youtube = build('youtube', 'v3', credentials=credentials)    request = youtube.playlistItems().insert(        part="snippet",        body={"snippet": {"playlistId": playlist_id,                #"position": 0,"resourceId": {"kind": "youtube#video","videoId": video_id                }            }        }    )    response = request.execute()    return(response)

I am however getting a Forbidden error message back from the Google API when attempting to add a new video:

Adding "VGmaMlmkTyU" to Playlist.Traceback (most recent call last):  File "C:/Users/back2/PycharmProjects/newScraper/main.py", line 124, in <module>    google_api_stuff.add_video(playlist_id, video_id, credentials)  File "C:\Users\back2\PycharmProjects\newScraper\google_api_stuff.py", line 94, in add_video    response = request.execute()  File "C:\Users\back2\PycharmProjects\newScraper\venv\lib\site-packages\googleapiclient\_helpers.py", line 131, in positional_wrapper    return wrapped(*args, **kwargs)  File "C:\Users\back2\PycharmProjects\newScraper\venv\lib\site-packages\googleapiclient\http.py", line 937, in execute    raise HttpError(resp, content, uri=self.uri)googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&alt=json returned "Forbidden". Details: "[{'message': 'Forbidden', 'domain': 'youtube.playlistItem', 'reason': 'playlistItemsNotAccessible'}]">Process finished with exit code 1

I'm not quite certain if there's some sort of code error or if there's something I've neglected to do in the Google Developer Console, but I'm not quite sure where to take this next in troubleshooting. Is anybody able to give me any pointers or suggestions please?

Thanks.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>