Forums

Trying to use spotipy authentication

When trying to authenticate with spotipy using the redirect http://localhost:8080, it does not redirect to the spotify login page, the site just continues to load forever.

I then tried with http://localhost:80 and I get the following message Python threads support is disabled. You can enable it with --enable-threads. Is there any work around for this?

If you redirect to localhost, that will redirect to the host that the browser is running on. If you want the redirect to work on PythonAnywhere, you need to redirect to your web app.

Now if I try to just redirect to the home page of my website, I get 'EOF when reading a line' and I can't get the credentials that I am trying to get from the spotify login.

I guess my question would be on how to set up a redirect uri to work with my web app?

I believe that the authentication flow that uses the redirect to localhost is designed for apps that run on your local machine, not on servers. They have a separate authentication flow for server-to-server authentication.

Ohh ok thank you

Hy, I have the same problem: for authentication with spotify, I have to fill a "redirect uri": an url. When the application runs locally, I specified a localhost url, but with python anywhere, if I understood correctly, I can't use this type of url. Which url can I use ? I used the url of my application (http://[user-name].pythonanywhere.com) as redirect uri but it doesn't work.

Thanks in advance !

Check out the link in my last post on this thread.

Hy thanks for your answer,

I already read this.

My problem is that the : SpotifyClientCredentials method doesn't give the authorizations to run my application. Indeed, I need to be able to create playlists for users, which requires an authorization code flow :

https://spotipy.readthedocs.io/en/2.12.0/#authorization-code-flow

What error are you getting? How does your code look like?

So I think the problem happens when I'm trying to get the token with the authorization code flow (those two following lines) :

token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)

sp = spotipy.Spotify(auth=token)

And I have this error :

2020-10-06 07:11:38,065: Couldn't read cache at: .cache-XXXXXXXXXXX
2020-10-06 07:11:38,066: 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.
2020-10-06 07:11:38,210: Opened https://accounts.spotify.com/authorize?client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&response_type=code&redirect_uri=http%3A%2F%2Ftestsr.pythonanywhere.com%2F&scope=playlist-modify-private+playlist-read-private+streaming+user-library-read+user-modify-playback-state+user-read-playback-state in your browser
2020-10-06 07:11:38,224: Exception on /_dash-update-component [POST]
Traceback (most recent call last):
  File "/home/testsr/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 130, in _get_user_input
    return raw_input(prompt)
NameError: name 'raw_input' is not defined
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/testsr/.local/lib/python3.7/site-packages/dash/dash.py", line 1059, in dispatch
    response.set_data(func(*args, outputs_list=outputs_list))
  File "/home/testsr/.local/lib/python3.7/site-packages/dash/dash.py", line 994, in add_context
    output_value = func(*args, **kwargs)  # %% callback invoked %%
  File "/home/testsr/mysite/app.py", line 344, in update_username
    redirect_uri = "http://testsr.pythonanywhere.com/")
  File "/home/testsr/.local/lib/python3.7/site-packages/spotipy/util.py", line 99, in prompt_for_user_token
    code = sp_oauth.get_auth_response()
  File "/home/testsr/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 448, in get_auth_response
    return self._get_auth_response_interactive(open_browser=open_browser)
  File "/home/testsr/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 398, in _get_auth_response_interactive
    response = self._get_user_input(prompt)
  File "/home/testsr/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 132, in _get_user_input
    return input(prompt)
EOFError: EOF when reading a line

That raw_input looks a bit odd -- that's a Python 2 function, and you're using 3.7. Are you sure you're using the most recent version of the Spotify API?

[edited by admin: code formatting]

14:36 ~ $ pip3.7 show spotipy
Name: spotipy
Version: 2.16.0
Summary: A light weight Python library for the Spotify Web API
Home-page: https://spotipy.readthedocs.org/
Author: @plamere
Author-email: paul@echonest.com
License: LICENSE.md
Location: /home/testsr/.local/lib/python3.7/site-packages
Requires: requests, six
Required-by:

Yes, the last version of spotipy is 2.16.0.

By the way, is it normal that the bash console is by default in python 2.7 and that I have to specify each time my version (pip3.7) ?

Sorry, but It seems that the method "Indent by 4 spaces for code blocks" on this forum, is'nt working now.

If spotipy is using raw_input, that suggests it is only written to run on Python 2.7 and not 3.7.

We have a number of Python versions installed. python and pip work with Python 2.7 and python3.7 and pip3.7 work with Python 3.7 etc.

It's not possible, my app is running well locally, and I'm on python 3.7

raw_input is definitely Python 2.7 function so you must be doing something different on your local machine.

Yes you're right, that's a Python 2.7 function.

I have explored the code of the Spotipy packages and I've got the problematic block :

@staticmethod
def _get_user_input(prompt):
    try:
        return raw_input(prompt)
    except NameError:
        return input(prompt)

Indeed, since the raw_input function is not defined in python 3.7, the system is supposed to execute the input function, which is defined in version 3.7. But it seems that this problem no longer occurs. We can see that on the new errors I get, we understand that the input function has been executed correctly.

2020-10-07 09:55:05,436: Couldn't read cache at: .cache-XXXXXXXXXXX
2020-10-07 09:55:05,437: 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.
2020-10-07 09:55:05,529: Opened https://accounts.spotify.com/authorize?client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback%2F&scope=playlist-modify-private+playlist-read-private+user-library-read+user-modify-playback-state+user-read-playback-state in your browser

Normally at this stage, a page opens in my browser. This is a confirmation request from Spotify informing that the application will have access to certain data and control possibilities.

Thanks, that explains the raw_input thing.

The problem with using this specific flow in the Spotipy library is that it's trying to open a browser tab on the machine where it is running. So if you have your server running locally, on your own machine, then it does that and a browser tab appears on the browser that is running on your computer. But if it tries it when the server is running on PythonAnywhere, it will try to open a browser on the computer where that browser is running -- that is, the server computer in our datacenter -- which, of course, won't work.

If there is some kind of Spotify capability where one of their users (that is, the person visiting your site) can grant access to a third-party server (that is, the website you're writing), then there must be another Spotipy flow for it -- but I can't see anything obvious in their docs. Perhaps you could ask on their GitHub page how you're meant to write that kind of site?

So.... nobody found a solution to this? Thanks.

We do not have a solution. This is the sort of thing where asking for help from either spotify or the authors of spotipy is much more likely to get you the help you need.