App Engine - 难以访问 Twitter 数据 - Flask
我在使用 Google App Engine 从 Twitter 搜索 API 获取数据时遇到问题。在个人开发环境中工作我没有任何问题。我能够得到我想要的 JSON。但是,一旦代码部署到 App Engine,我就会遇到 HTTP 500 错误代码。
我对正在发生的事情的唯一想法是,我所缺少的开发环境和实时 App Engine 环境之间存在一些根本区别,并且/或 Twitter 拒绝了来自我运行的 App Engine IP 的请求。我写了一个小@app.route来绝对验证是否存在问题 - 如下。
我很确定我没有达到速率限制,因为 Twitter 搜索 API 没有明确设置速率限制。我看到他们请求了一个用户代理,所以我一直在提供,但我仍然收到相同的 500 错误。
我正在使用的实时版本在这里: http://1-alpha- 3.rich90usa.appspot.com/twitter_test
如果您对这里出现的问题有任何想法,我们将不胜感激。
Flask 中的相关部分:
@app.route('/twitter_test')
def twitter_test():
twitter_geo_url = 'http://search.twitter.com/search.json?callback=?&rpp=100&geocode=29.6516344,-82.3248262,3mi'
twitter_result = urlfetch.fetch(twitter_geo_url, fetch_headers)
if twitter_result.status_code == 200:
return twitter_result.content
其中“fetch_headers”先前定义为:
fetch_headers = {'User-Agent': "APPNAME-1-alpha-3"}
我的 Flask py 文件的顶部:
from flask import Flask, request, render_template, session, redirect
import oauth2 as oauth
import simplejson as json
import urlparse
import urllib
import httplib
import time
from google.appengine.api import urlfetch
I'm having problems getting data from the Twitter search API with Google App Engine. Working in the personal development environment I have no problems. I'm able to get the JSON I want. However, once the code gets deployed to App Engine, I encounter a HTTP 500 Error Code.
The only ideas I have for whats going on is that there is some fundamental difference between the dev and live App Engine environments that I'm missing and/or that Twitter is refusing requests from the App Engine IP I'm running from. I wrote a small @app.route to absolutely verify that there are issues - is is below.
I'm pretty sure I'm not hitting a rate limit because the Twitter Search API doesn't explicitly have one. I saw that they requested a user-agent so I've been providing that but I'm still getting the same 500 Error.
The live version of what I'm working with is here: http://1-alpha-3.rich90usa.appspot.com/twitter_test
Any thoughts on what's going wrong here would be greatly appreciated.
Relevant Section from Flask:
@app.route('/twitter_test')
def twitter_test():
twitter_geo_url = 'http://search.twitter.com/search.json?callback=?&rpp=100&geocode=29.6516344,-82.3248262,3mi'
twitter_result = urlfetch.fetch(twitter_geo_url, fetch_headers)
if twitter_result.status_code == 200:
return twitter_result.content
Where 'fetch_headers' is previously defined as:
fetch_headers = {'User-Agent': "APPNAME-1-alpha-3"}
Top of my Flask py file:
from flask import Flask, request, render_template, session, redirect
import oauth2 as oauth
import simplejson as json
import urlparse
import urllib
import httplib
import time
from google.appengine.api import urlfetch
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Calvin 指出的那样,所有 App Engine 应用程序都从同一 IP 池发出出站 HTTP 请求,因此问题几乎肯定是速率限制:
来自 http://dev.twitter.com/pages/rate-limiting:
他们还特别指出 GAE 没有资格列入白名单:
Twitter 针对绕过速率限制的建议是切换到Streaming API。
As Calvin indicated, all App Engine apps source outbound HTTP requests from the same pool of IPs, so the issue is almost certainly rate limiting:
From http://dev.twitter.com/pages/rate-limiting:
They also call out GAE specifically as being ineligible for whitelisting:
Twitter's suggestion to get around rate-limiting is to switch to the Streaming API.