2

I'm writing a console application to create a playlist on Spotify according to a list of song. I'm using Spotipy and I'm trying with util.prompt_for_user_token(), the problem is that after I grant access I have to manually paste the redirect link into the console.

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.

Is there a way to automate this process? The solution here didn't help me

Alessandro Gaballo
  • 708
  • 3
  • 13
  • 28
  • I ran into the same exact issue while running [sconsify](http://www.sconsify.org/), I don't think there is an easy way to do this. – Woohoojin Aug 07 '18 at 14:39

1 Answers1

0

The prompt_for_user_token function is a shorthand for some functionality provided by Spotipy. Here's the code of the function itself.

https://github.com/plamere/spotipy/blob/master/spotipy/util.py

[ -- snip -- ]

def prompt_for_user_token(username, scope=None, client_id = None,
    client_secret = None, redirect_uri = None, cache_path = None):
''' prompts the user to login if necessary and returns
    the user token suitable for use with the spotipy.Spotify 
    constructor
    Parameters:
     - username - the Spotify username
     - scope - the desired scope of the request
     - client_id - the client id of your app
     - client_secret - the client secret of your app
     - redirect_uri - the redirect URI of your app
     - cache_path - path to location to save tokens
'''

[ -- snip -- ]

cache_path = cache_path or ".cache-" + username
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, 
    scope=scope, cache_path=cache_path)

[ -- snip -- ]

token_info = sp_oauth.get_cached_token()

if not token_info:
    print('''
        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.
    ''')
    auth_url = sp_oauth.get_authorize_url()
    try:
        import webbrowser
        webbrowser.open(auth_url)
        print("Opened %s in your browser" % auth_url)
    except:
        print("Please navigate here: %s" % auth_url)

[ -- snip -- ]

You can replicate the code and use auth_url (for example) with popen module and chrome.exe, passing auth_url as an argument.

TIP: Under windows cmd.exe /c start http://your.website/address will forward the address to your current default browser. (as opposed to hardcoding firefox.exe or chrome.exe)

loa_in_
  • 1,030
  • 9
  • 20
  • From what you're saying I suppose there's no way to retrieve the "redirected url" with `webbrowser`, so I need to find a way to do it manually, is that right? – Alessandro Gaballo Aug 07 '18 at 14:52
  • Just write your own `prompt_for_user_token` function based on the one I quoted. Instead of `webbrowser.open(auth_url)` use `return auth_url`, and you have a function that returns you your URL. There is also another issue here, you could try installing `webbrowser` python module with `pip` and it won't default to `print("Please navigate here: %s" % auth_url)`. – loa_in_ Aug 07 '18 at 14:54
  • Currently `webbrowser.open(auth_url)` doesn't work apparently, since code jumps to `except:` code there – loa_in_ Aug 07 '18 at 14:55
  • `webbrowser.open(auth_url)` is working for me, the problem is that after I log into Spotify I'm redirected to some url that I need to paste in the console, I still haven't figured how to automatically get that url – Alessandro Gaballo Aug 08 '18 at 07:14
  • I can only point you further. Try this: https://askubuntu.com/questions/929686/get-url-of-current-active-tab-from-firefox-via-command-line – loa_in_ Aug 08 '18 at 09:15