[GH-ISSUE #834] Is there any way to use Github Actions Python testing while using Authorization Code Flow? #512

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

Originally created by @MarijnStam on GitHub (Jun 26, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/834

Hello all,

I have recently tried to set up Github Actions to automatically perform my tests with pytest whenever a push happens.
A lot of these tests revolve around interacting with spotipy and use the Authorization Code Flow.
For this I use the redirect URI: http://localhost:8080 to handle the URL prompt for authentication. Locally, this works fine and all tests work. However in the GitHub Action, the test using spotipy will hang indefinitely. I verified that the enviroment variables are properly set for the automatic tests.
I assume I can't handle the URL prompt the same way as I do it locally, as I can't just open a webserver on the Github Actions container (I think?).
Is there any workaround for this? I would like to have my tests automatically ran on a push.

Thanks!

Originally created by @MarijnStam on GitHub (Jun 26, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/834 Hello all, I have recently tried to set up Github Actions to automatically perform my tests with pytest whenever a push happens. A lot of these tests revolve around interacting with spotipy and use the Authorization Code Flow. For this I use the redirect URI: `http://localhost:8080` to handle the URL prompt for authentication. Locally, this works fine and all tests work. However in the GitHub Action, the test using spotipy will hang indefinitely. I verified that the enviroment variables are properly set for the automatic tests. I assume I can't handle the URL prompt the same way as I do it locally, as I can't just open a webserver on the Github Actions container (I think?). Is there any workaround for this? I would like to have my tests automatically ran on a push. Thanks!
kerem 2026-02-27 23:23:03 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@harshvardhaniimi commented on GitHub (Aug 24, 2023):

I am running into this issue as well. I am not creating a public app, just for my own usecase. But it seems to run into this problem where Github Actions will crash after trying it for six hours.

<!-- gh-comment-id:1692383781 --> @harshvardhaniimi commented on GitHub (Aug 24, 2023): I am running into this issue as well. I am not creating a public app, just for my own usecase. But it seems to run into this problem where Github Actions will crash after trying it for six hours.
Author
Owner

@MarijnStam commented on GitHub (Aug 25, 2023):

Thank you for reporting that you run into the same issue. I have not found a solution yet for this issue since reporting, although I have not put much time into it.

<!-- gh-comment-id:1693019537 --> @MarijnStam commented on GitHub (Aug 25, 2023): Thank you for reporting that you run into the same issue. I have not found a solution yet for this issue since reporting, although I have not put much time into it.
Author
Owner

@harshvardhaniimi commented on GitHub (Aug 25, 2023):

Yesterday, I was able to execute my code with Github Actions, thanks to help from ChatGPT. Essentially, the function refresh_access_token obtains a new access token from Spotify using a refresh token. Inside the function, client_id and client_secret are concatenated and base64 encoded to create the authorization header, and a POST request is made to Spotify's token endpoint with a payload containing the refresh token. The function returns the new access token, which is then used to authenticate a Spotipy client instance. This allows me to use it without reauthenticating via browser. Works like a charm!

import spotipy
import requests
import base64
import os

# Function to refresh access token
def refresh_access_token(refresh_token):
    client_id = os.environ['SPOTIPY_CLIENT_ID']
    client_secret = os.environ['SPOTIPY_CLIENT_SECRET']
    payload = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token,
    }
    auth_header = {'Authorization': 'Basic ' + base64.b64encode((client_id + ':' + client_secret).encode()).decode()}
    response = requests.post('https://accounts.spotify.com/api/token', data=payload, headers=auth_header)
    return response.json().get('access_token')

client_id = os.environ['SPOTIPY_CLIENT_ID']
client_secret = os.environ['SPOTIPY_CLIENT_SECRET']
refresh_token = os.environ['SPOTIPY_REFRESH_TOKEN']
new_access_token = refresh_access_token(refresh_token)

# Set up Spotipy
sp = spotipy.Spotify(auth=new_access_token)
<!-- gh-comment-id:1693501396 --> @harshvardhaniimi commented on GitHub (Aug 25, 2023): Yesterday, I was able to execute my code with Github Actions, thanks to help from ChatGPT. Essentially, the function `refresh_access_token` obtains a new access token from Spotify using a refresh token. Inside the function, `client_id` and `client_secret` are concatenated and base64 encoded to create the authorization header, and a `POST` request is made to Spotify's token endpoint with a payload containing the refresh token. The function returns the new access token, which is then used to authenticate a Spotipy client instance. This allows me to use it without reauthenticating via browser. Works like a charm! ```{python} import spotipy import requests import base64 import os # Function to refresh access token def refresh_access_token(refresh_token): client_id = os.environ['SPOTIPY_CLIENT_ID'] client_secret = os.environ['SPOTIPY_CLIENT_SECRET'] payload = { 'grant_type': 'refresh_token', 'refresh_token': refresh_token, } auth_header = {'Authorization': 'Basic ' + base64.b64encode((client_id + ':' + client_secret).encode()).decode()} response = requests.post('https://accounts.spotify.com/api/token', data=payload, headers=auth_header) return response.json().get('access_token') client_id = os.environ['SPOTIPY_CLIENT_ID'] client_secret = os.environ['SPOTIPY_CLIENT_SECRET'] refresh_token = os.environ['SPOTIPY_REFRESH_TOKEN'] new_access_token = refresh_access_token(refresh_token) # Set up Spotipy sp = spotipy.Spotify(auth=new_access_token) ```
Author
Owner

@MarijnStam commented on GitHub (Aug 29, 2023):

Thanks a bunch for this update! I'll be sure to get back to this when I pick up this project again.

<!-- gh-comment-id:1697472935 --> @MarijnStam commented on GitHub (Aug 29, 2023): Thanks a bunch for this update! I'll be sure to get back to this when I pick up this project again.
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#512
No description provided.