Question

I am parsing urls from vimeo/twitch/youtube etc in chat. Everything works fine, except for when I switched from scraping youtube to actually using their api. Now this is a really small app so there's no need for huge client libs and oAuth 2 etc. Just using python with urllib2.

Now, if I open this specific url in my browser, I get data back, but when I do it on my server I get 403 Forbidden. According to google developer console it has worked 2/10 times when querying from my server.

    https://www.googleapis.com/youtube/v3/videos?id={id}&key={api_key}&part=snippet,contentDetails,statistics&fields=items(snippet/channelTitle,snippet/title,contentDetails/duration,statistics/viewCount)

This is the url that my app creates. When I open this in my browser and plot in the api key etc it works just fine, but when I do:

    urllib2.urlopen(url)

I get 403 Forbidden.

The api key is generated from "Public API access" -> "Server key" in the Google Developers Console. Which has IP whitelisting, and the server ip is in there.

Anyone have idea why this is happening? Only reason I can think of is because the header that urllib2 sends tells youtube the request is coming from python, but that shouldn't be an issue when you have an api key though?

Was it helpful?

Solution

When using urllib2 you need to make sure you have urlencoded all your parameters ... if there's a character in your key that is getting munged because it isn't urlencoded that could lead to the 403.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top