【问题标题】:Unable to add YouTube playlist using v3 api无法使用 v3 api 添加 YouTube 播放列表
【发布时间】:2012-12-21 20:16:05
【问题描述】:

我已经为此工作了一段时间,但似乎无法越过这个障碍。

我可以使用 v3 api 创建服务,并且可以取回一些特定于用户的数据,但是在添加播放列表时,我遇到了一个似乎无法解决的错误。

--编辑-- 传递对象而不是 jsonified 字符串将起作用。

json_obj = {'snippet':{'title':title}}
#json_str = json.dumps(json_obj)
playlist = self.service.playlists().insert(part='snippet, status', body=json_obj)
playlist.execute()

这给了我这样的东西:

请求标头:

{'Authorization': u'Bearer TOKEN',
 'accept': 'application/json',
 'accept-encoding': 'gzip, deflate',
 'content-length': '73',
 'content-type': 'application/json',
 'user-agent': 'google-api-python-client/1.0'}

请求正文:

'"{\\"snippet\\":{\\"title\\":\\"2013newTest\\"}}"'

响应标头:

{'cache-control': 'private, max-age=0',
 'content-type': 'application/json; charset=UTF-8',
 'date': 'Tue, 08 Jan 2013 01:40:13 GMT',
 'expires': 'Tue, 08 Jan 2013 01:40:13 GMT',
 'server': 'GSE',
 'status': '400',
 'transfer-encoding': 'chunked',
 'x-content-type-options': 'nosniff',
 'x-frame-options': 'SAMEORIGIN',
 'x-xss-protection': '1; mode=block'}

响应正文:

'{"error": {
   "errors": [
     {"domain": "youtube.parameter",
      "reason": "missingRequiredParameter",
      "message": "No filter selected.", 
      "locationType": "parameter",
      "location": ""}
             ],
  "code": 400,
  "message": "No filter selected."}}'

图书馆因此引发的响应:

Traceback (most recent call last):
  File "playlist.py", line 190, in <module>
    yt_pl.add_playlist('2013newTest')
  File "playlist.py", line 83, in add_playlist
    playlist.execute()
  File "oauth2client/util.py", line 121, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "apiclient/http.py", line 693, in execute
    raise HttpError(resp, content, uri=self.uri)
apiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/youtube/v3/playlists?alt=json&part=snippet%2C+status&key=[KEY] returned "No filter selected.">

我能找到的唯一的东西是在 C# 中有人遇到相同错误的地方只是模糊相关。有没有人能够在 python 中使用 v3 添加播放列表,如果可以,你能看到我做错了什么吗?

【问题讨论】:

  • 尝试首先在 Google API 资源管理器中运行:developers.google.com/apis-explorer/#p/youtube/v3 我已经使用资源管理器发出了类似(经过身份验证的)请求,并获得了 200 OK。
  • 我能够从那里得到一个有效的回复,这让我认为这是他们的库中的某些东西或者我做错了什么。我似乎在他们的文档或发现过程用于创建对象本身的 json 中找不到任何引用过滤器的内容。
  • 您如何验证对象?您是否尝试过在不将对象转储到 JSON 的情况下执行?
  • 使用他们的 oAuth2 库 - 使用 OAuth2WebServerFlow 和从站点下载的 client_secrets 和 googleapis.com/auth/youtube 作为范围。我正在为我用来测试的帐户手动授权它,然后缓存凭据。我可以取回有效的“授权”数据——比如对 playlists.list 使用 mine=True 参数。例如,这很好用: get_all_playlists = self.service.playlists().list(part='id, sn-p, status', mine=True) pl_data = get_all_playlists.execute()
  • 事实证明可以修复它 - 如果您直接将它传递而不是转换为 json 字符串,它就可以工作。

标签: python youtube youtube-api google-api-python-client


【解决方案1】:

body 中发送的有效载荷必须是可以序列化为 JSON 的对象。

这是因为默认的 JsonModel used for your request 正文有一个 serialize 方法,它总是 dumps 到 json:

class JsonModel(BaseModel):
  ...
  def serialize(self, body_value):
    if (isinstance(body_value, dict) and 'data' not in body_value and
        self._data_wrapper):
      body_value = {'data': body_value}
    return simplejson.dumps(body_value)

所以当你传入已经序列化的 JSON 字符串时,你会得到双重序列化。

例如:

>>> json.dumps({'a': 'b'})
'{"a": "b"}'
>>> json.dumps('{"a": "b"}')
'"{\\"a\\": \\"b\\"}"'

这基本上就是您的请求正文发生的情况:

'"{\\"snippet\\":{\\"title\\":\\"2013newTest\\"}}"'

您能否指出一些导致您误入歧途的文档以便对其进行修复?

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 2014-02-12
    • 2016-09-17
    • 2015-08-18
    • 1970-01-01
    • 2017-11-01
    • 2015-12-31
    • 2021-03-24
    • 2014-07-02
    相关资源
    最近更新 更多