App Engine - 难以访问 Twitter 数据 - Flask

发布于 2024-11-03 17:29:39 字数 1345 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-11-10 17:29:39

正如 Calvin 指出的那样,所有 App Engine 应用程序都从同一 IP 池发出出站 HTTP 请求,因此问题几乎肯定是速率限制:

来自 http://dev.twitter.com/pages/rate-limiting

但是,
所有来自某个 IP 地址的请求
应用于搜索速率限制。
未设置搜索率限制
公众劝阻不必要的
搜索使用和滥用,但它是
高于 REST 速率限制。我们
感觉搜索率限制是两者
自由且足以满足大多数人的需要
应用程序并知道许多
应用程序供应商已经发现了
适合他们的需求。

他们还特别指出 GAE 没有资格列入白名单:

应用程序必须具有静态 IP 才有资格列入白名单。这意味着大多数云平台(包括 Google App Engine)无法列入白名单。

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:

However,
all requests coming from an IP address
are applied to a Search Rate Limit.
The Search Rate Limit isn't made
public to discourage unnecessary
search usage and abuse, but it is
higher than the REST Rate Limit. We
feel the Search Rate Limit is both
liberal and sufficient for most
applications and know that many
application vendors have found it
suitable for their needs.

They also call out GAE specifically as being ineligible for whitelisting:

An application must have a static IP to be eligible for whitelisting. This means the majority of cloud platforms, including Google App Engine, cannot be whitelisted.

Twitter's suggestion to get around rate-limiting is to switch to the Streaming API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文