2013-01-08 77 views
3

我一直在研究這一點,似乎無法通過此塊。無法使用v3 api添加YouTube播放列表

我可以使用v3 api創建服務,並可以獲得一些用戶特定的數據,但是當涉及到添加播放列表時,我收到一個我似乎無法解決的錯誤。

--EDIT--傳遞對象而不是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添加播放列表,如果有的話,你能看到我在做什麼錯了嗎?

+0

試着首先在Google API資源管理器中使用此工具:https://developers.google.com/apis-explorer/#p/youtube/v3/我使用資源管理器進行了類似(已驗證)的請求,得到200 OK。 – bossylobster

+0

我可以從那裏得到一個有效的迴應,這導致我認爲它是在lib或者我做錯了的事情。我似乎無法在其文檔或json中找到引用過濾器的任何內容,以便發現過程用它來創建對象本身。 –

+0

您如何驗證對象?你有沒有嘗試執行沒有轉儲對象到JSON? – bossylobster

回答

3

body中發送的有效載荷必須是可以序列化爲JSON的對象。

這是因爲默認JsonModelused 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\\"}}"' 

你能指出一些文件,導致你引入歧途,因此可以解決嗎?

相關問題