[GH-ISSUE #905] SpotifyOAuth in bots #545

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

Originally created by @Syzygianinfern0 on GitHub (Nov 30, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/905

I'm trying to use the add_tracks_to_playlist.py in a bot which needs to run periodically by itself.

When I run it locally with the right ID, Secret, Redirect URl, and Scope it opens the browser window where I can authenticate it and my code runs perfectly. When I tried to run it on my bot though it throws an error trying to open the webpage. How do I work around that? I tried putting the .cache file from my local run in the bot's filesystem but I think it expires after some time as it ran yesterday but not today.

Here is the code snippet I used and the complete stack trace
    scope = "playlist-modify-public"
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(id, secret, "http://example.com", scope=scope))
    urls = [each[0] for each in new_tracks.items]
    [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)]
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.[8](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:9)/site-packages/spotipy/oauth2.py", line 115, in _get_user_input
    return raw_input(prompt)
NameError: name 'raw_input' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 88, in <module>
    main()
  File "main.py", line 75, in main
    [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)]
  File "main.py", line 75, in <listcomp>
    [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)]
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 1040, in playlist_add_items
    return self._post(
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 302, in _post
    return self._internal_call("POST", url, payload, kwargs)
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 535, in get_access_token
    "code": code or self.get_auth_response(),
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 4[9](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:10)9, in get_auth_response
    return self._get_auth_response_interactive(open_browser=open_browser)
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 450, in _get_auth_response_interactive
    response = self._get_user_input(prompt)
  File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line [11](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:12)7, in _get_user_input
    return input(prompt)
EOFError: EOF when reading a line
Enter the URL you were redirected to:
Originally created by @Syzygianinfern0 on GitHub (Nov 30, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/905 I'm trying to use the [add_tracks_to_playlist.py](https://github.com/spotipy-dev/spotipy/blob/master/examples/add_tracks_to_playlist.py) in a bot which needs to run periodically by itself. When I run it locally with the right ID, Secret, Redirect URl, and Scope it opens the browser window where I can authenticate it and my code runs perfectly. When I tried to run it on my bot though it throws an error trying to open the webpage. How do I work around that? I tried putting the `.cache` file from my local run in the bot's filesystem but I think it expires after some time as it ran yesterday but not today. <details> <summary>Here is the code snippet I used and the complete stack trace</summary> ```python scope = "playlist-modify-public" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(id, secret, "http://example.com", scope=scope)) urls = [each[0] for each in new_tracks.items] [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)] ```` ``` Traceback (most recent call last): File "/home/runner/.local/lib/python3.[8](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:9)/site-packages/spotipy/oauth2.py", line 115, in _get_user_input return raw_input(prompt) NameError: name 'raw_input' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 88, in <module> main() File "main.py", line 75, in main [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)] File "main.py", line 75, in <listcomp> [sp.playlist_add_items(TARGET, urls[idx : idx + 100]) for idx in range(0, len(urls), 100)] File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 1040, in playlist_add_items return self._post( File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 302, in _post return self._internal_call("POST", url, payload, kwargs) File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 221, in _internal_call headers = self._auth_headers() File "/home/runner/.local/lib/python3.8/site-packages/spotipy/client.py", line 212, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 535, in get_access_token "code": code or self.get_auth_response(), File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 4[9](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:10)9, in get_auth_response return self._get_auth_response_interactive(open_browser=open_browser) File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 450, in _get_auth_response_interactive response = self._get_user_input(prompt) File "/home/runner/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line [11](https://github.com/Syzygianinfern0/CI-is-my-DJ/actions/runs/3579226185/jobs/6020205424#step:6:12)7, in _get_user_input return input(prompt) EOFError: EOF when reading a line Enter the URL you were redirected to: ``` </details> <!--- Please make sure you've: - read the FAQ https://github.com/plamere/spotipy/blob/master/FAQ.md - read the documentation https://spotipy.readthedocs.io/en/latest/ - searched older issues If your question is about code, please share the code you are using --->
kerem 2026-02-27 23:23:15 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@ShirinovAdil commented on GitHub (May 15, 2023):

@Syzygianinfern0 figured out a solution?

<!-- gh-comment-id:1547839422 --> @ShirinovAdil commented on GitHub (May 15, 2023): @Syzygianinfern0 figured out a solution?
Author
Owner

@dieser-niko commented on GitHub (May 15, 2023):

I don't think there is an elegant solution to this problem. I don't know if it's possible to have permanent access to an account, so the best solution is to create some sort of page that you would have to visit from time to time to re-authenticate.
Here's an example with flask: https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py

<!-- gh-comment-id:1547869664 --> @dieser-niko commented on GitHub (May 15, 2023): I don't think there is an elegant solution to this problem. I don't know if it's possible to have permanent access to an account, so the best solution is to create some sort of page that you would have to visit from time to time to re-authenticate. Here's an example with flask: https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py
Author
Owner

@dieser-niko commented on GitHub (May 21, 2024):

Hi, just a quick follow up as I'm trying to resolve/close some old issues.
If you want to use headless auth, just follow this example: https://github.com/spotipy-dev/spotipy/blob/master/examples/headless.py

The previously suggested app.py example is still a valid solution if you prefer a webserver for authentication.

I'm going to close the issue for now, but feel free to re-open it if you have any questions.

<!-- gh-comment-id:2123472873 --> @dieser-niko commented on GitHub (May 21, 2024): Hi, just a quick follow up as I'm trying to resolve/close some old issues. If you want to use headless auth, just follow this example: https://github.com/spotipy-dev/spotipy/blob/master/examples/headless.py The previously suggested [app.py](https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py) example is still a valid solution if you prefer a webserver for authentication. I'm going to close the issue for now, but feel free to re-open it if you have any questions.
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#545
No description provided.