[GH-ISSUE #513] Can't perform authorization, SET is invalid on Windows #308

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

Originally created by @janecafe on GitHub (Jun 15, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/513

Hi,
I am trying to authorize the Spotipy API and it is not working. I have the following (removed sensitive data)

SET SPOTIPY_CLIENT_ID='myid'
SET SPOTIPY_CLIENT_SECRET='mysecret'
SET SPOTIPY_REDIRECT_URI='http://localhost:1410/'

The above way, I get invalid syntax. The more straightforward way below also does not work:

import spotipy
import spotipy.util as util

token = util.prompt_for_user_token(
        username='myuser',
        scope='user-library-read',
        client_id='myid',
        client_secret=mysecret',
        redirect_uri='http://localhost:1410/')


Originally created by @janecafe on GitHub (Jun 15, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/513 Hi, I am trying to authorize the Spotipy API and it is not working. I have the following (removed sensitive data) ``` SET SPOTIPY_CLIENT_ID='myid' SET SPOTIPY_CLIENT_SECRET='mysecret' SET SPOTIPY_REDIRECT_URI='http://localhost:1410/' ``` The above way, I get invalid syntax. The more straightforward way below also does not work: ``` import spotipy import spotipy.util as util token = util.prompt_for_user_token( username='myuser', scope='user-library-read', client_id='myid', client_secret=mysecret', redirect_uri='http://localhost:1410/') ```
kerem closed this issue 2026-02-27 23:21:55 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Jun 15, 2020):

Hi @jane-dempsey,

There might be a mistake in the doc, can you try set instead of SET?

About the error with util.prompt_for_user_token, can you please describe it? cheers

<!-- gh-comment-id:644146566 --> @stephanebruckert commented on GitHub (Jun 15, 2020): Hi @jane-dempsey, There might be a mistake in the doc, can you try `set` instead of `SET`? About the error with `util.prompt_for_user_token`, can you please describe it? cheers
Author
Owner

@janecafe commented on GitHub (Jun 15, 2020):

Hi @jane-dempsey,

There might be a mistake in the doc, can you try set instead of SET?

About the error with util.prompt_for_user_token, can you please describe it? cheers

Thanks for chiming in!
I have tried both set and SET.

For the second batch of code utilizing util -- when I run it, a new page pops it. I get this error:

This site can’t be reachedlocalhost refused to connect.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

Regardless, I post the URL of this page in the text box that pops up in my IDE (Jupyter Notebook).

I tried authorizing around a year ago, and recall with some attempts a page would pop up via Spotify Developer asking me to authorize. In other cases, just this error page would pop up.

In my current attempts, when I get this localhost error, I post the URL in the textbox but when I try other things it says my client ID is missing and needs to be authorized.

The current redirect URIs in my Spotify Developer page are http://localhost:1410/ and http://localhost/ -- I have tried both.

<!-- gh-comment-id:644170376 --> @janecafe commented on GitHub (Jun 15, 2020): > Hi @jane-dempsey, > > There might be a mistake in the doc, can you try `set` instead of `SET`? > > About the error with `util.prompt_for_user_token`, can you please describe it? cheers Thanks for chiming in! I have tried both set and SET. For the second batch of code utilizing util -- when I run it, a new page pops it. I get this error: ``` This site can’t be reachedlocalhost refused to connect. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED ``` Regardless, I post the URL of this page in the text box that pops up in my IDE (Jupyter Notebook). I tried authorizing around a year ago, and recall with some attempts a page would pop up via Spotify Developer asking me to authorize. In other cases, just this error page would pop up. In my current attempts, when I get this localhost error, I post the URL in the textbox but when I try other things it says my client ID is missing and needs to be authorized. The current redirect URIs in my Spotify Developer page are http://localhost:1410/ and http://localhost/ -- I have tried both.
Author
Owner

@stephanebruckert commented on GitHub (Jun 15, 2020):

Hmm I'm really not sure why set wouldn't work. Can you enter just set alone and see if they appear in the list?


This site can’t be reached
localhost refused to connect.

I think this message is expected. At this point you should copy the URL and paste it into your terminal.

However, there is a way to do that automatically by using a port (as opposed to using simply localhost. See https://spotipy.readthedocs.io/en/2.12.0/#redirect-uri

If your request URI is set to http://127.0.0.1:<port> or http://localhost:<port> Spotipy will automatically complete the authorization process (obtain the <code> from the URI).

If your request URI is set to any other URI you will need to manually copy the URI from the browser’s address bar and paste it into the terminal/console where your Spotipy script is running (Spotipy will instruct you to do so).

<!-- gh-comment-id:644267459 --> @stephanebruckert commented on GitHub (Jun 15, 2020): Hmm I'm really not sure why `set` wouldn't work. Can you enter just `set` alone and see if they appear in the list? -------------- This site can’t be reached localhost refused to connect. I think this message is expected. At this point you should copy the URL and paste it into your terminal. However, there is a way to do that automatically by using a port (as opposed to using simply `localhost`. See https://spotipy.readthedocs.io/en/2.12.0/#redirect-uri > If your request URI is set to `http://127.0.0.1:<port>` or `http://localhost:<port>` Spotipy will automatically complete the authorization process (obtain the `<code>` from the URI). > >If your request URI is set to any other URI you will need to manually copy the URI from the browser’s address bar and paste it into the terminal/console where your Spotipy script is running (Spotipy will instruct you to do so).
Author
Owner

@janecafe commented on GitHub (Jun 15, 2020):

When I try
set SPOTIPY_CLIENT_ID='myclientid'
I get a Syntax error.
Also, I did paste the URL in the terminal as I've typically been doing. When I try to perform other tasks, I get the error message in my IDE:

SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3A2Cellos&limit=10&offset=0&type=artist:
 No token provided

No matter how many times I try to authorize, I get a notice saying there is no token :(

<!-- gh-comment-id:644284268 --> @janecafe commented on GitHub (Jun 15, 2020): When I try `set SPOTIPY_CLIENT_ID='myclientid' ` I get a Syntax error. Also, I did paste the URL in the terminal as I've typically been doing. When I try to perform other tasks, I get the error message in my IDE: ``` SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3A2Cellos&limit=10&offset=0&type=artist: No token provided ``` No matter how many times I try to authorize, I get a notice saying there is no token :(
Author
Owner

@stephanebruckert commented on GitHub (Jun 15, 2020):

What is the exact syntax error when using set?
Are you running this from the Windows command line?
Are you running it with or without single quotes? if one does not work, I would try the other one

Also please share your full code. This 401 error message is often seen if the token was not passed to Spotify(), such as:

token = util.prompt_for_user_token(...)
if token:
    sp = spotipy.Spotify(auth=token)
<!-- gh-comment-id:644293657 --> @stephanebruckert commented on GitHub (Jun 15, 2020): What is the exact syntax error when using `set`? Are you running this from the Windows command line? Are you running it with or without single quotes? if one does not work, I would try the other one Also please share your full code. This 401 error message is often seen if the token was not passed to Spotify(), such as: ``` token = util.prompt_for_user_token(...) if token: sp = spotipy.Spotify(auth=token) ```
Author
Owner

@janecafe commented on GitHub (Jun 15, 2020):

See below, I removed my personal data like my username, client id, client secret.
I am doing this work in Jupyter Notebook IDE, it is my understanding this is fine.

Before I go to my current full code, separate to that is the error I get with set:

SET SPOTIPY_CLIENT_ID='MYCLIENTID'

error message:


 File "<ipython-input-1-3ac4eed6e067>", line 1
    SET SPOTIPY_CLIENT_ID='bf24c6fa1c704008954b3d6649d8361c'
                        ^
SyntaxError: invalid syntax

That was from a previous attempt so I currently am trying with no set, see below:

import os
import sys
import json
import spotipy
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError

username = 'MYUSERNAME
scope = 'user-read-currently-playing'
redirect_uri = 'http://localhost/'

CLIENT_ID = 'MYCLIENTID'
CLIENT_SECRET = 'MYCLIENTSECRET'

token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri)

When I implement the token, a new Google Chrome page pops up with this error:

This site can’t be reachedlocalhost refused to connect.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

I then paste the URL in the IDE -- a text box appears asking me to fill in the URL. Here is the output:

User authentication requires interaction with your
            web browser. Once you enter your credentials and
            give authorization, you will be redirected to
            a url.  Paste that url you were directed to to
            complete the authorization.

        
Opened https://accounts.spotify.com/authorize?client_id=bf24c6fa1c704008954b3d6649d8361c&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&scope=user-read-currently-playing in your browser


Enter the URL you were redirected to: http://localhost/?code=AQCNZCV8rwe8THi5ysQ0Wel_ETueRuBCMGOC-Yx9ZDkKCJX98TaCrn_uagB9KulXf4-skeEreOd7PSqSJa8zvPhPwebZMQFbSe-qVITnbB5OnZf7vNINhToSKrCnzZqZPd24vHbC5OwJsYgLrDaCLxXfNYJWtmGL3Hnvf5OcWBhlieHKFeU5rgVqdrTqYczA-tI

Then, when I try this simple piece of code:

name = '2Cellos'
spotify = spotipy.Spotify()
results = spotify.search(q='artist:' + name, type='artist')
print(results)

I get this error:

SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3A2Cellos&limit=10&offset=0&type=artist:
 No token provided
<!-- gh-comment-id:644309231 --> @janecafe commented on GitHub (Jun 15, 2020): See below, I removed my personal data like my username, client id, client secret. I am doing this work in Jupyter Notebook IDE, it is my understanding this is fine. Before I go to my current full code, separate to that is the error I get with set: `SET SPOTIPY_CLIENT_ID='MYCLIENTID'` error message: ``` File "<ipython-input-1-3ac4eed6e067>", line 1 SET SPOTIPY_CLIENT_ID='bf24c6fa1c704008954b3d6649d8361c' ^ SyntaxError: invalid syntax ``` That was from a previous attempt so I currently am trying with no set, see below: ``` import os import sys import json import spotipy import webbrowser import spotipy.util as util from json.decoder import JSONDecodeError username = 'MYUSERNAME scope = 'user-read-currently-playing' redirect_uri = 'http://localhost/' CLIENT_ID = 'MYCLIENTID' CLIENT_SECRET = 'MYCLIENTSECRET' token = util.prompt_for_user_token(username, scope, CLIENT_ID, CLIENT_SECRET, redirect_uri) ``` When I implement the token, a new Google Chrome page pops up with this error: ``` This site can’t be reachedlocalhost refused to connect. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED ``` I then paste the URL in the IDE -- a text box appears asking me to fill in the URL. Here is the output: ``` User authentication requires interaction with your web browser. Once you enter your credentials and give authorization, you will be redirected to a url. Paste that url you were directed to to complete the authorization. Opened https://accounts.spotify.com/authorize?client_id=bf24c6fa1c704008954b3d6649d8361c&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&scope=user-read-currently-playing in your browser Enter the URL you were redirected to: http://localhost/?code=AQCNZCV8rwe8THi5ysQ0Wel_ETueRuBCMGOC-Yx9ZDkKCJX98TaCrn_uagB9KulXf4-skeEreOd7PSqSJa8zvPhPwebZMQFbSe-qVITnbB5OnZf7vNINhToSKrCnzZqZPd24vHbC5OwJsYgLrDaCLxXfNYJWtmGL3Hnvf5OcWBhlieHKFeU5rgVqdrTqYczA-tI ``` Then, when I try this simple piece of code: ``` name = '2Cellos' spotify = spotipy.Spotify() results = spotify.search(q='artist:' + name, type='artist') print(results) ``` I get this error: ``` SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3A2Cellos&limit=10&offset=0&type=artist: No token provided ```
Author
Owner

@stephanebruckert commented on GitHub (Jun 15, 2020):

  • this is how you should set environment variables on Jupyter Notebook https://stackoverflow.com/a/44251637/1515819
  • your code is not correct, when you do spotipy.Spotify() you should actually do spotipy.Spotify(auth=token)
<!-- gh-comment-id:644311046 --> @stephanebruckert commented on GitHub (Jun 15, 2020): - this is how you should set environment variables on Jupyter Notebook https://stackoverflow.com/a/44251637/1515819 - your code is not correct, when you do `spotipy.Spotify()` you should actually do `spotipy.Spotify(auth=token)`
Author
Owner

@janecafe commented on GitHub (Jun 17, 2020):

It works! Thank you so much for all of your help!

<!-- gh-comment-id:645102939 --> @janecafe commented on GitHub (Jun 17, 2020): It works! Thank you so much for all of your help!
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#308
No description provided.