YouTube API v2 / feeds / api / videos / VIDEOID在200多个请求后失败:400 Bad Request

时间:2013-06-21 20:23:22

标签: youtube-api feeds bad-request

我发布了大约7000个视频,需要制作大约500个“未上市”视频。首先,我想查看这些视频的列表,并查看他们当前的“访问”'动作''列出'状态。

我使用YouTube V2 API,对我的内容进行OAUTH身份验证。然后我使用此查询循环显示视频ID列表:

    url = 'http://gdata.youtube.com/feeds/api/videos/' + youtube_id + '?alt=json'

在大约214个请求之后,所有后续请求都失败了:

<HTML>
  <HEAD>
    <TITLE>Bad Request</TITLE>
  </HEAD>
  <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Bad Request</H1>
    <H2>Error 400</H2>
  </BODY>
</HTML>

headers={'status': '400', 'content-length': '145', 'x-xss-protection': '1; mode=block', 'x-content-type-options': 'nosniff', 'expires': 'Fri, 21 Jun 2013 20:08:28 GMT', 'server': 'GSE', 'cache-control': 'private, max-age=0', 'date': 'Fri, 21 Jun 2013 20:08:28 GMT', 'x-frame-options': 'SAMEORIGIN', 'content-type': 'text/html; charset=UTF-8'} 

没有详细说明为什么这个,以及剩下的~300,都失败了。

这是可重复的,如果我重新运行它会一次又一次地在第214次之后死亡。如果我从序列的中途开始,跳过第一个212,它就会在第426个视频中消失 - 所以视频#215不是孤立的问题。

这听起来像是我达到了配额,但是我们已经把我们的配件搞得一团糟,而且API控制台让我们无法接近我们的极限。

有什么想法吗?

我还没有开始写改变,所以这很令人担忧。 感谢。

1 个答案:

答案 0 :(得分:0)

在针对API运行查询之前,我已使用OAUTH登录并创建了一个HTTP请求对象。我的修复是定期重新调用登录,我现在每10次查询一次 - 这似乎已经足够了。

FWIW,我的身份验证例程(基于Posnick的例子)和调用如下所示:

class YouTubeV2(object):
    """Authenticate to APIv2 and do stuff we can't do with v3, like captions
    """
    OAUTH_SCOPE = "https://gdata.youtube.com"

    def __init__(self, client_id, client_secret, developer_key):
        """OAuth authenticates, creates 'http' attribute to make a .request().

        Get client_id, client secret from: http://code.google.com/apis/console
        Get developer_key from: http://code.google.com/apis/youtube/dashboard
        """
        # This auth code is generic and should be used for any APIv2 calls.
        self.client_id = client_id
        self.client_secret = client_secret
        self.developer_key = developer_key
        self.headers = {'GData-Version': '2',
                        'X-GData-Key': 'key=%s' % self.developer_key}
        storage = Storage("%s-oauth" % sys.argv[0])
        self.credentials = storage.get()
        if self.credentials is None or self.credentials.invalid:
            # If there are no valid cached credentials, take the user through the
            # OAuth2 login flow, and rely on the client library to cache the
            # credentials once that's complete.
            flow = OAuth2WebServerFlow(
                client_id=self.client_id,
                client_secret=self.client_secret,
                scope=self.OAUTH_SCOPE,
                user_agent=sys.argv[0])
            self.credentials = run(flow, storage)
        self.http = self.credentials.authorize(httplib2.Http())

yt2 = YouTubeV2(client_id, client_secret, developer_key)