Beispiel #1
0
def refresh_user_token(spotify_user):
    """ Refreshes the user token for the given spotify user.

    Args:
        spotify_user (domain.spotify.Spotify): the user whose token is to be refreshed

    Returns:
        user (domain.spotify.Spotify): the same user with updated tokens
    """
    auth = get_spotify_oauth()

    retries = SPOTIFY_API_RETRIES
    new_token = None
    while retries > 0:
        new_token = auth.refresh_access_token(spotify_user.refresh_token)
        if new_token:
            break
        retries -= 1
    if new_token is None:
        raise SpotifyAPIError('Could not refresh API Token for Spotify user')

    access_token = new_token['access_token']
    refresh_token = new_token['refresh_token']
    expires_at = new_token['expires_at']
    db_spotify.update_token(spotify_user.user_id, access_token, refresh_token,
                            expires_at)
    return get_user(spotify_user.user_id)
def refresh_user_token(spotify_user):
    """ Refreshes the user token for the given spotify user.

    Args:
        spotify_user (domain.spotify.Spotify): the user whose token is to be refreshed

    Returns:
        user (domain.spotify.Spotify): the same user with updated tokens
    """
    auth = get_spotify_oauth()

    retries = SPOTIFY_API_RETRIES
    new_token = None
    while retries > 0:
        new_token = auth.refresh_access_token(spotify_user.refresh_token)
        if new_token:
            break
        retries -= 1
    if new_token is None:
        raise SpotifyAPIError('Could not refresh API Token for Spotify user')

    access_token = new_token['access_token']
    refresh_token = new_token['refresh_token']
    expires_at = new_token['expires_at']
    db_spotify.update_token(spotify_user.user_id, access_token, refresh_token, expires_at)
    return get_user(spotify_user.user_id)
Beispiel #3
0
 def test_update_token(self):
     old_spotify_user = db_spotify.get_user(self.user['id'])
     db_spotify.update_token(
         user_id=self.user['id'],
         access_token='testtoken',
         refresh_token='refreshtesttoken',
         expires_at=int(time.time()),
     )
     spotify_user = db_spotify.get_user(self.user['id'])
     self.assertEqual(spotify_user['user_token'], 'testtoken')
     self.assertEqual(spotify_user['refresh_token'], 'refreshtesttoken')
 def test_update_token(self):
     old_spotify_user = db_spotify.get_user(self.user['id'])
     db_spotify.update_token(
         user_id=self.user['id'],
         access_token='testtoken',
         refresh_token='refreshtesttoken',
         expires_at=int(time.time()),
     )
     spotify_user = db_spotify.get_user(self.user['id'])
     self.assertEqual(spotify_user['user_token'], 'testtoken')
     self.assertEqual(spotify_user['refresh_token'], 'refreshtesttoken')
Beispiel #5
0
def refresh_user_token(spotify_user: Spotify):
    """ Refreshes the user token for the given spotify user.

    Args:
        spotify_user (domain.spotify.Spotify): the user whose token is to be refreshed

    Returns:
        user (domain.spotify.Spotify): the same user with updated tokens
        None: if the user has revoked authorization to spotify

    Raises:
        SpotifyAPIError: if unable to refresh spotify user token

    Note: spotipy eats up the json body in case of error but we need it for checking
    whether the user has revoked our authorization. hence, we use our own
    code instead of spotipy to fetch refresh token.
    """
    retries = SPOTIFY_API_RETRIES
    response = None
    while retries > 0:
        response = _get_spotify_token("refresh_token",
                                      spotify_user.refresh_token)

        if response.status_code == 200:
            break
        elif response.status_code == 400:
            error_body = response.json()
            if "error" in error_body and error_body["error"] == "invalid_grant":
                # user has revoked authorization through spotify ui or deleted their spotify account etc.
                # in any of these cases, we should delete user from our spotify db as well.
                db_spotify.delete_spotify(spotify_user.user_id)
                return None

            response = None  # some other error during request

        retries -= 1

    if response is None:
        raise SpotifyAPIError('Could not refresh API Token for Spotify user')

    response = response.json()
    access_token = response['access_token']
    if "refresh_token" in response:
        refresh_token = response['refresh_token']
    else:
        refresh_token = spotify_user.refresh_token
    expires_at = int(time.time()) + response['expires_in']
    db_spotify.update_token(spotify_user.user_id, access_token, refresh_token,
                            expires_at)
    return get_user(spotify_user.user_id)
def refresh_user_token(spotify_user):
    """ Refreshes the user token for the given spotify user.

    Args:
        spotify_user (domain.spotify.Spotify): the user whose token is to be refreshed

    Returns:
        user (domain.spotify.Spotify): the same user with updated tokens
    """
    auth = get_spotify_oauth()
    new_token = auth.refresh_access_token(spotify_user.refresh_token)
    access_token = new_token['access_token']
    refresh_token = new_token['refresh_token']
    expires_at = new_token['expires_at']
    db_spotify.update_token(spotify_user.user_id, access_token, refresh_token, expires_at)
    return get_user(spotify_user.user_id)