0

I have a Flask app where I want to create playlists using Spotify API. My issue is similar to this Stackoverflow question. The difference is that I am using OAuthlib instead of requests and the solution posted there didn't work in my case.

The problem

In the http request, when I set data={'name': 'playlist_name', 'description': 'something'}, I am getting a response: "error": {"status": 400,"message": "Error parsing JSON."}

But when I follow the answer mentioned above and try this: data=json.dumps({'name': 'playlist_name', 'description': 'something'}), I am getting following error in the console: "ValueError: not enough values to unpack (expected 2, got 1)".

How I can fix this? Here is a simplified version of my app:

app.py

from flask import Flask, url_for, session
from flask_oauthlib.client import OAuth

import json

app = Flask(__name__)
app.secret_key = 'development'
oauth = OAuth(app)

spotify = oauth.remote_app(
    'spotify',
    consumer_key=CLIENT,
    consumer_secret=SECRET,
    request_token_params={'scope': 'playlist-modify-public playlist-modify-private'},
    base_url='https://accounts.spotify.com',
    request_token_url=None,
    access_token_url='/api/token',
    authorize_url='https://accounts.spotify.com/authorize'
)

@app.route('/', methods=['GET', 'POST'])
def index():
    callback = url_for(
        'create_playlist',
        _external=True
    )
    return spotify.authorize(callback=callback)


@app.route('/playlist', methods=['GET', 'POST'])
def create_playlist():
    resp = spotify.authorized_response()
    session['oauth_token'] = (resp['access_token'], '')


    username = USER
    return spotify.post('https://api.spotify.com/v1/users/' + username + '/playlists',
                                   data=json.dumps({'name': 'playlist_name', 'description': 'something'}))


@spotify.tokengetter
def get_spotify_oauth_token():
    return session.get('oauth_token')


if __name__ == '__main__':
    app.run()

Ewelina Luczak
  • 379
  • 4
  • 13

1 Answers1

1

You are using the data parameter, which takes a dict object, but you are dumping it to a string, which is not necessary. Also, you have to set the format to json, as follow:

@app.route('/playlist', methods=['GET', 'POST'])
def create_playlist():
    resp = spotify.authorized_response()
    session['oauth_token'] = (resp['access_token'], '')


    username = USER
    return spotify.post('https://api.spotify.com/v1/users/' + username + '/playlists',
                                   data={'name': 'playlist_name', 'description': 'something'}, format='json')
Lucas Mello
  • 342
  • 3
  • 8
  • I tried this and received `TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a OAuthResponse.` :/ – Ewelina Luczak Jun 26 '21 at 21:05
  • Ah, I mistakenly returned spotify.post(....), corrected it. But unfotunately it doesn't work either, now I am getting http 400 error parsing JSON. – Ewelina Luczak Jun 26 '21 at 21:17
  • I edited my answer with the "format" parameter. Try this, please. – Lucas Mello Jun 26 '21 at 21:22
  • Now I am getting `AttributeError: 'dict' object has no attribute 'data'`. I wanted figure it out myself first, but it seems I really can't – Ewelina Luczak Jun 26 '21 at 22:01
  • Strange, but I checked again and it works; playlists have been created and I don't get any errors. Thank you! – Ewelina Luczak Jun 26 '21 at 22:18