Spotipy: TypeError in the oauth2.py - Can't create token

Created on 23 Apr 2018  路  5Comments  路  Source: plamere/spotipy

Hello,
I'm trying to run a simple example that returns my spotify playlists, but it seems I can't get pass the authentication phase succesfully. In particular, the script requests from me to enter the URL that redirects me, which when inserted, the following error is occured:

Traceback (most recent call last):
  File "my_playlists.py", line 19, in <module>
    token = util.prompt_for_user_token(username, scope)
  File "/opt/anaconda3/lib/python3.6/site-packages/spotipy-2.4.4-py3.6.egg/spotipy/util.py", line 88, in prompt_for_user_token
    def get_level_16(data, bits, verbose=False):
  File "/opt/anaconda3/lib/python3.6/site-packages/spotipy-2.4.4-py3.6.egg/spotipy/oauth2.py", line 212, in get_access_token
  File "/opt/anaconda3/lib/python3.6/site-packages/spotipy-2.4.4-py3.6.egg/spotipy/oauth2.py", line 195, in _make_authorization_headers
  File "/opt/anaconda3/lib/python3.6/site-packages/spotipy-2.4.4-py3.6.egg/spotipy/oauth2.py", line 20, in _make_authorization_headers
TypeError: must be str, not NoneType

The script I try to run is a very simple one, taken from the example section:


# Shows the top artists for a user

import pprint
import sys

import spotipy
import spotipy.util as util
import simplejson as json

if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print("Usage: %s username" % (sys.argv[0],))
    sys.exit()

scope = 'user-library-read'

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    sp.trace = False
    results = sp.current_user_playlists(limit=50)
    for i, item in enumerate(results['items']):
        print("%d %s" %(i, item['name']))
else:
    print("Can't get token for", username)

I'm currently running on python 3.6.3 on a Linux machine. Any suggestions?

question

All 5 comments

What is the URL you are entering?

I have used http://localhost/ as calback in spotify developer.

I think there is a problem with the way you are authenticating yourself. I believe it should be something like:


token = util.prompt_for_user_token(
        username=USERNAME,
        scope=SCOPE,
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        redirect_uri=REDIRECT_URI)

Ensure that you have exported your redirect_uri, client ID, and secret before running your script and trying to authenticate. If you don't pass the values directly to the util.prompt_for_user_token function, it will read them from the environment.

Try running these in your terminal before running your script

export SPOTIPY_CLIENT_ID="YOUR CLIENT ID"
export SPOTIPY_CLIENT_SECRET="YOUR CLIENT SECRET"
export SPOTIPY_REDIRECT_URI='http://localhost/'

Thanks for your answers @ritiek and @alopexc0de, that seems right, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielgwilson picture danielgwilson  路  4Comments

Kurchunk picture Kurchunk  路  3Comments

gawaineo picture gawaineo  路  3Comments

IzHoBX picture IzHoBX  路  4Comments

CMoore97 picture CMoore97  路  3Comments