[GH-ISSUE #279] TypeError in the oauth2.py - Can't create token #152

Closed
opened 2026-02-27 23:21:06 +03:00 by kerem · 5 comments
Owner

Originally created by @basophobic on GitHub (Apr 22, 2018).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/279

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?

Originally created by @basophobic on GitHub (Apr 22, 2018). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/279 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?
kerem 2026-02-27 23:21:06 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@kylegbakker commented on GitHub (Apr 23, 2018):

What is the URL you are entering?

<!-- gh-comment-id:383434335 --> @kylegbakker commented on GitHub (Apr 23, 2018): What is the URL you are entering?
Author
Owner

@basophobic commented on GitHub (Apr 23, 2018):

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

<!-- gh-comment-id:383490078 --> @basophobic commented on GitHub (Apr 23, 2018): I have used http://localhost/ as calback in spotify developer.
Author
Owner

@ritiek commented on GitHub (Apr 23, 2018):

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)
<!-- gh-comment-id:383496527 --> @ritiek commented on GitHub (Apr 23, 2018): I think there is a problem with the way you are authenticating yourself. I believe it should be something like: ```python token = util.prompt_for_user_token( username=USERNAME, scope=SCOPE, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI) ```
Author
Owner

@c0de-fox commented on GitHub (Apr 24, 2018):

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/'

<!-- gh-comment-id:384113288 --> @c0de-fox commented on GitHub (Apr 24, 2018): 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/'`
Author
Owner

@stephanebruckert commented on GitHub (Jan 1, 2020):

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

<!-- gh-comment-id:570082608 --> @stephanebruckert commented on GitHub (Jan 1, 2020): Thanks for your answers @ritiek and @alopexc0de, that seems right, closing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#152
No description provided.