I’ve run into this issue in the past, and have been able to work around it until now. When making a call to the Twitch API in app engine, I get an Error 400. I’ve been to the google groups twitch page, as well as twitch tv’s own forum.
I was able to talk to a ton of people at Twitch TV as well as App Engine, and to no luck have I been able to solve this problem. In hopes of clarifying my question, I have been able to make a simple app to replicate directly what is occurring when I try to make a URL call to Twitch TV’s API.
Summary:
This is the simple code that fails when my application is deployed, but works flawlessly in the localhost environment:
import webapp2
import urllib2
class MainHandler(webapp2.RequestHandler):
def get(self):
url = ('https://api.twitch.tv/kraken/streams/nl_kripp')
contents = urllib2.urlopen(url)
self.response.out.write(contents.read())
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
Here is what this exact application generates when being run on localhost (as it is suppose too):
{“stream”:{“name”:”live_user_nl_kripp”,”game”:”Guild Wars 2″,”viewers”:3229,”_links”:{“self”:”https://api.twitch.tv/kraken/streams/nl_kripp”},”_id”:3681678672,”broadcaster”:”fme”,”channel”:{“game”:”Guild Wars 2″,”name”:”nl_kripp”,”created_at”:”2012-04-15T02:25:31Z”,”teams”:[{“name”:”nolife”,”created_at”:”2012-06-20T23:48:51Z”,”background”:null,”updated_at”:”2012-06-20T23:49:08Z”,”banner”:null,”_links”:{“self”:”https://api.twitch.tv/kraken/teams/nolife”},”_id”:330,”logo”:null,”info”:”\n”,”display_name”:”noLife”}],”banner”:null,”updated_at”:”2012-08-26T21:12:55Z”,”background”:”http://static-cdn.jtvnw.net/jtv_user_pictures/nl_kripp-channel_background_image-527e8b792a46df22.png”,”url”:”http://www.twitch.tv/nl_kripp”,”logo”:”http://static-cdn.jtvnw.net/jtv_user_pictures/nl_kripp-profile_image-267fd5e2fb95a15d-300×300.png”,”_id”:29795919,”_links”:{“stream_key”:”https://api.twitch.tv/kraken/channels/nl_kripp/stream_key”,”self”:”https://api.twitch.tv/kraken/channels/nl_kripp”,”chat”:”https://api.twitch.tv/kraken/chat/nl_kripp”,”commercial”:”https://api.twitch.tv/kraken/channels/nl_kripp/commercial”,”features”:”https://api.twitch.tv/kraken/channels/nl_kripp/features”},”mature”:null,”video_banner”:”http://static-cdn.jtvnw.net/jtv_user_pictures/nl_kripp-channel_offline_image-f3ad1124bc19cffd-640×360.png”,”display_name”:”nl_Kripp”,”status”:”nolife Kripparrian — GW2 Ranger Lvling. Server = Maguuma”},”preview”:”http://static-cdn.jtvnw.net/previews/live_user_nl_kripp-630×473.jpg”,”partner”:true},”_links”:{“self”:”https://api.twitch.tv/kraken/streams/nl_kripp”}}
And now the same exact application in the App Engine environment:
http://urltestingsite.appspot.com/
I have spent quite a while trying to solve this problem as it is a major chain link in my website. Does anyone, have any remote idea as to why this occurs?
EDIT – this is the code from my actual application:
def check_if_live(self, b):
url = ('https://api.twitch.tv/kraken/streams/%s' %b)
url2 = urlfetch.fetch(url, headers = {'User-Agent': "suitegamer_User-Agent"})
contents = urllib2.urlopen(url2)
if (contents.read()).find('{"stream":null,') == 0:
return 'Offline'
else:
return 'Live'
This code is returning this AttributeError:
AttributeError: '_URLFetchResult' object has no attribute 'get_type'
This errors are returned some times when you hit the rate limit or when your app is denied access.
According to this thread, it may be what could actually be happening. Since GAE is a shared environment, you will most certainly hit these limits very often, because there could be many users querying the twitch.tv and/or justin.tv (I don’t know if the limits are shared between the two).
Also according to this other thread, the api just don’t like the GAE production User-Agent (I tried it myself and the problem is still there).
The solution to the second problem is to set the User-Agent (see How to change User-Agent on Google App Engine UrlFetch service?), but you will still probably be hitting the rate limits.