1

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?

3
  • I'm not saying this is the solution, but 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. Commented Apr 10, 2014 at 1:40
  • Wow, this fixed the problem! Thanks a lot man, can you put this as an answer instead of a comment so I can accept it? :) Commented Apr 10, 2014 at 3:14
  • Glad it was the problem ... been bitten by that one myself! Moved the comment to an answer, and hopefully it'll help others as well. Commented Apr 10, 2014 at 4:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.