[GH-ISSUE #980] Authorization Code Flow: Unable to enter URL in production/EOF error #588

Closed
opened 2026-02-27 23:23:30 +03:00 by kerem · 2 comments
Owner

Originally created by @maxmir20 on GitHub (Jun 2, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/980

Hello,

I am using Spotipy to make my first Django Web App to return a User's currently playing track when a GET API is called with the User's id (on my app, not on the Spotify API). My code works no problem locally, but when I launch it to production in Digital Ocean, no new browser window opens with the Redirect URI and I run into a EOF error in runtimes logs where it doesn't allow me to handle raw_input in the

@staticmethod def _get_user_input(prompt): try: return raw_input(prompt)

static method.

I also don't have the same level of console access as I would locally, so even if the redirect callback opened in a new tab, I don't think I would be able to enter it into the console.

I've included SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, and SPOTIPY_REDIRECT_URI as environment variables, which should be called by CLIENT_CREDS_ENV_VARS (I can confirm the information is correct by looking at the logger.debug in runtime logs). And am calling Spotipy.Oauth2 as follows:

print("unable to find credentials")
SCOPE = ["user-read-currently-playing"]
sp_oath = SpotifyOAuth(
                scope=SCOPE
            )

token_info = sp_oath.get_access_token(as_dict=True)
print(f'adding new credentials {token_info}')
profile = Profile.objects.get(id=host_info.profile.id)
credentials = Credential(profile=profile, encrypted_token=token_info.get('refresh_token'))
credentials.save()

Do you have any idea what I could do to solve this problem in production? Either by sidestepping the "Enter URL" step or getting the new window authorization working?

Originally created by @maxmir20 on GitHub (Jun 2, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/980 Hello, I am using Spotipy to make my first Django Web App to return a User's currently playing track when a GET API is called with the User's id (on my app, not on the Spotify API). My code works no problem locally, but when I launch it to production in Digital Ocean, no new browser window opens with the Redirect URI and I run into a EOF error in runtimes logs where it doesn't allow me to handle raw_input in the `@staticmethod def _get_user_input(prompt): try: return raw_input(prompt)` static method. I also don't have the same level of console access as I would locally, so even if the redirect callback opened in a new tab, I don't think I would be able to enter it into the console. I've included SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, and SPOTIPY_REDIRECT_URI as environment variables, which should be called by CLIENT_CREDS_ENV_VARS (I can confirm the information is correct by looking at the logger.debug in runtime logs). And am calling Spotipy.Oauth2 as follows: ``` print("unable to find credentials") SCOPE = ["user-read-currently-playing"] sp_oath = SpotifyOAuth( scope=SCOPE ) token_info = sp_oath.get_access_token(as_dict=True) print(f'adding new credentials {token_info}') profile = Profile.objects.get(id=host_info.profile.id) credentials = Credential(profile=profile, encrypted_token=token_info.get('refresh_token')) credentials.save() ``` Do you have any idea what I could do to solve this problem in production? Either by sidestepping the "Enter URL" step or getting the new window authorization working?
kerem 2026-02-27 23:23:30 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@Avishkar15 commented on GitHub (Jun 19, 2023):

Hey did you find a solution

<!-- gh-comment-id:1597018566 --> @Avishkar15 commented on GitHub (Jun 19, 2023): Hey did you find a solution
Author
Owner

@maxmir20 commented on GitHub (Jun 19, 2023):

@Avishkar15 ,

I did find a solution but it was by sidestepping the Spotipy process and going through the authentication myself on Django.

I have one API make the authorization request, which opens the window for them to authorize their account. Then when it's complete, the authorize API returns the callback URL. I redirect to that callback URL and have an API in place to grab it and then use the 'code' parameter from the URL to request the access token

def authorize_spotify_user(request, userID):
    print('starting spotify authorization')
    url = 'https://accounts.spotify.com/authorize'
    params = {
        'response_type': 'code',
        'client_id': settings.CLIENT_ID,
        'scope': SCOPE,
        'redirect_uri': settings.REDIRECT_URI,
    }
    spotify_request = requests.get(url=url, params=params)
    print(spotify_request.url)
    return HttpResponseRedirect(spotify_request.url)


def request_access_token(request):
    print('now that we have our callback, request access token')
    print(request)
    print(request.GET)

    auth_header = base64.urlsafe_b64encode((settings.CLIENT_ID + ':' + settings.CLIENT_SECRET).encode())
    url = 'https://accounts.spotify.com/api/token'
    headers = {
        "Authorization": f"Basic {auth_header.decode('ascii')}",
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    params = {
        'grant_type': 'authorization_code',
        'code': request.GET.get('code'),
        'redirect_uri': settings.REDIRECT_URI
    }
    response = requests.post(url=url, params=params, headers=headers)
<!-- gh-comment-id:1597086360 --> @maxmir20 commented on GitHub (Jun 19, 2023): @Avishkar15 , I did find a solution but it was by sidestepping the Spotipy process and going through the authentication myself on Django. I have one API make the authorization request, which opens the window for them to authorize their account. Then when it's complete, the authorize API returns the callback URL. I redirect to that callback URL and have an API in place to grab it and then use the 'code' parameter from the URL to request the access token ``` def authorize_spotify_user(request, userID): print('starting spotify authorization') url = 'https://accounts.spotify.com/authorize' params = { 'response_type': 'code', 'client_id': settings.CLIENT_ID, 'scope': SCOPE, 'redirect_uri': settings.REDIRECT_URI, } spotify_request = requests.get(url=url, params=params) print(spotify_request.url) return HttpResponseRedirect(spotify_request.url) def request_access_token(request): print('now that we have our callback, request access token') print(request) print(request.GET) auth_header = base64.urlsafe_b64encode((settings.CLIENT_ID + ':' + settings.CLIENT_SECRET).encode()) url = 'https://accounts.spotify.com/api/token' headers = { "Authorization": f"Basic {auth_header.decode('ascii')}", 'Content-Type': 'application/x-www-form-urlencoded' } params = { 'grant_type': 'authorization_code', 'code': request.GET.get('code'), 'redirect_uri': settings.REDIRECT_URI } response = requests.post(url=url, params=params, headers=headers) ```
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#588
No description provided.