Adi Shakti
Adi Shakti

Reputation: 33

Unable to add YouTube playlist using v3 api

I've been working on this for a bit and can't seem to get past this block.

I can create the service using the v3 api and can get some user specific data back but when it comes to adding playlists I'm getting an error that I can't seem to get around.

--EDIT-- Passing the object instead of the jsonified string will work.

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

Which gives me something like this:

Request Headers:

{'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'}

Request Body:

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

Response Headers:

{'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'}

Response Body:

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

And the response the library raises as a result:

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.">

The only thing I could find where someone was getting the same error was only vaguely related and was in C#. Has anyone been able to add playlists using v3 in python and if so can you see what I'm doing wrong?

Upvotes: 3

Views: 1139

Answers (1)

bossylobster
bossylobster

Reputation: 10163

The payload sent in body must be an object which can be serialized into JSON.

This is because the default JsonModel used for your request body has a serialize method which always dumps to 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)

So when you pass in already JSON serialized string, you get double serialized.

For example:

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

Which is essentially what happened to your request body:

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

Could you point to some documentation that led you astray so it can be fixed?

Upvotes: 3

Related Questions