【问题标题】:GAE Generating HTTP Error 400: Bad Request Traceback When Indexing URL from Twitch.tv APIGAE 生成 HTTP 错误 400:从 Twitch.tv API 索引 URL 时出现错误的请求回溯
【发布时间】:2012-08-22 01:44:13
【问题描述】:

我似乎在部署应用程序时收到此错误,但在 localhost 上提供服务时却没有:

E 2012-08-21 18:03:46.914 HTTP Error 400: Bad Request Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py

E 2012-08-21 18:03:46.916 Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 195, in Handle

产生此错误的代码位于此处(请原谅庞大的处理程序):

class Dashboard(MainHandler):

def check_if_live(self,b):
    url = ('http://api.justin.tv/api/stream/list.json?channel=%s' %b)
    contents = urllib2.urlopen(url)
    if contents.read() == '[]':
        return 'Offline'
    else:
        return 'Live'

def get(self, profile_id):
    u = User.by_name(profile_id.lower())
    if not u:
        self.redirect('/')
    elif self.user.name != profile_id:
        self.redirect('/profile/%s' %self.user.name)
    elif self.user and profile_id:
        current_user = self.user.name
        name1 = ''
        friend_name = ''
        team_imagee = ''
        key = ''
        monthss = ''
        yearss = ''
        dayss = ''
        countryss = ''
        team_imagee2 = ''
        imgs2 = ''
        imgs = ''
        key2 = ''
        name22 = ''
        name2 = ''
        streamss = ''
        streams_title = ''
        imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", profile_id)
        imgs2  = db.GqlQuery("select * from Profile_Images WHERE name =:1", current_user)
        team_name  = db.GqlQuery("select * from Teams WHERE name =:1", profile_id)
        team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", profile_id)
        team_images2  = db.GqlQuery("select * from Teamimg WHERE user =:1", current_user)
        friends  = db.GqlQuery("select * from Friends WHERE name =:1 order by added_date desc limit 10", profile_id)
        posts = db.GqlQuery("select * from Profile_Comments WHERE name_of_profile =:1 order by date_created desc", profile_id)
        name2 = db.GqlQuery("select * from User WHERE name =:1", profile_id)
        month = db.GqlQuery("select * from User WHERE name =:1", profile_id)
        day = db.GqlQuery("select * from User WHERE name =:1", profile_id)
        year = db.GqlQuery("select * from User WHERE name =:1", profile_id)
        country = db.GqlQuery("select * from User WHERE name =:1", profile_id)
        streams = db.GqlQuery("select * from Streams WHERE username =:1", current_user)
        for stream_title in streams:
            streams_title = stream_title.stream_title
        for stream in streams:
            streamss = stream.stream
        for months in month:
            monthss = months.month
        for countrys in country:
            countryss = countrys.country
        for days in day:
            dayss = days.day
        for years in year:
            yearss = years.year
        for clan in team_name:
                name1 = clan.team_name_anycase
        for clan in team_name:
                name2 = clan.team_name
        for image in team_images:
            team_imagee = image.key()
        for image2 in team_images2:
            team_imagee2 = image2.key()
        for img in imgs:
            key = img.key()
        for img2 in imgs2:
            key2 = img2.key()
        streamm = (streamss[(streamss.find('.tv/')+ 4):])
        check_if_life2 = self.check_if_live((streamss[(streamss.find('.tv/')+ 4):]))
        self.render('dashboard.html', streams = streams, stream_title2 = streamm, stream_title = streams_title, check_if_life = check_if_life2, team_name2 = name2,  imgs = imgs, team_img2 = team_imagee2, profile_image_posted = key2, posts = posts, profile_id = profile_id, country_var = countryss, day_var = dayss, year_var = yearss, month_var = monthss, current_user = current_user, friends = friends, team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
    else:
        self.redirect('/register')

这就是导致它的具体原因:

    streamm = (streamss[(streamss.find('.tv/')+ 4):])
    check_if_life2 = self.check_if_live((streamss[(streamss.find('.tv/')+ 4):]))

它利用了顶部的函数“check_if_live”。作为一个完全的 Python 菜鸟,我一生都无法弄清楚为什么这个错误是实时生成的,而不是在本地主机上生成的。

编辑:

我仍然无法让 API 工作。有趣的是,如果您在地址栏中调用 API,例如 http://api.justin.tv/api/stream/list.json?channel=nasltv,它可以正常工作。但是一旦它接触到应用程序引擎,它就会失败。我只需要索引这个 API!啊!

【问题讨论】:

    标签: python api google-app-engine


    【解决方案1】:

    您调用的 API 是否涉及身份验证?也许有一个重定向 cookie。 IIRC prod 和 dev 之间的重定向行为不同。

    【讨论】:

    • 这似乎是一个身份验证问题。你介意我问一下为什么服务器会将来自我本地机器的请求解释为与本例中的应用引擎不同的东西吗?
    • 服务器可能无法分辨。可能 SDK 中的 urlfetch 实现在重定向上的行为与生产 urlfetch 实现不同。我记得之前回答过这个问题:stackoverflow.com/questions/9420795/…——也许它有一些提示。还有stackoverflow.com/questions/10288942/…
    • 太棒了。我会做一些阅读。我还向 API 支持团队发送了一封电子邮件。任何人都非常感谢你!干杯!
    • FWIW,App Engine 总是将一些固定字符串附加到 User-Agent。如果 Twitch.tv 不喜欢这个,他们的问题。 (OTOH,我们可能应该在开发应用服务器中做同样的事情。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多