Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to use Spotify inside an API? #287

Closed
Jeoffreybauvin opened this issue May 10, 2018 · 4 comments
Closed

Best way to use Spotify inside an API? #287

Jeoffreybauvin opened this issue May 10, 2018 · 4 comments

Comments

@Jeoffreybauvin
Copy link

Hi,

I'm using Spotipy in a Flask API (full rest). I'm trying to use util.prompt_for_user_token for login, and it's working.

But now, I want to create an entrypoint /authenticate, which send me back the URL that util.prompt_for_user_token is trying to open in my browser.

I don't see any information in the documentation to do so. Can I catch this URL without opening my browser ?

@MaZderMind
Copy link
Contributor

MaZderMind commented Jan 21, 2020

@Jeoffreybauvin In the Case described by you I would not use util.prompt_for_user_token, because it clearly states to be a utility to get you up and running quickly. When Implementing a custom RESt-API, I would instead operate the required Client-Classes directly.

When removing all the input-checking and comments from util.prompt_for_user_token there is not a lot left:

def redirect_user_to_oauth(username, scope=None, client_id=None,
                          client_secret=None, redirect_uri=None,
                          cache_path=None):

…
    sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
                                   scope=scope, cache_path=cache_path)
…
    token_info = sp_oauth.get_cached_token()
    if not token_info:
        auth_url = sp_oauth.get_authorize_url()
        return auth_url


def handle_callback(code):
    token_info = sp_oauth.get_access_token(code)
    return token_info['access_token']

As an API-Implementor you have to provide the requisite options to oauth2.SpotifyOAuth and have it request an auth_url for you. Depending on the Implementation of your REST-API I would return this URL to my client in order for it to open it in a popup or otherwise have my user access it.

After the OAuth2 dance, the user is redirected to the redirect_uri you provided, which should be some url in your application (it might also be a local server if your writing a native app). The supplied code is then transferred to your REST-API in a second call, which can use it to request token_info on you users behalf.

@stephanebruckert stephanebruckert changed the title Best way to use Spotify inside an API ? Best way to use Spotify inside an API? Feb 22, 2020
@stephanebruckert
Copy link
Member

stephanebruckert commented Feb 27, 2020

For now this provides good inspiration to solve the issue #435 (comment)

@stephanebruckert
Copy link
Member

Just added an example here https://github.com/plamere/spotipy/blob/master/examples/app.py

@andrewhansen89
Copy link

Just added an example here https://github.com/plamere/spotipy/blob/master/examples/app.py

Doesn't this example need a cache_path or username?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants