Skip to content

Commit

Permalink
Add authorisation token authentication method (#1580)
Browse files Browse the repository at this point in the history
* Add authorisation token authentication method

spotipy already allows the use of a static authorisation token as well
as authentication managers, this commit simply adds the configuration
options to pass the token through to spotipy.

* Update docs for authorisation token option

* Fix tests for authorisation token option
  • Loading branch information
patrickjonesuk authored Aug 22, 2022
1 parent 46a8fa7 commit 9a69845
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ If you don't want config to load automatically change `load_config` option in co
"overwrite": "skip",
"client_id": "5f573c9620494bae87890c0f08a60293",
"client_secret": "212476d9b0f3472eaa762d90b19b0ba8",
"auth_token": null,
"user_auth": false,
"search_query": null,
"filter_results": true,
Expand Down Expand Up @@ -263,6 +264,8 @@ Spotify options:
The client id to use when logging in to Spotify.
--client-secret CLIENT_SECRET
The client secret to use when logging in to Spotify.
--auth-token AUTH_TOKEN
The authorisation token to use directly to log in to Spotify.
--cache-path CACHE_PATH
The path where spotipy cache file will be stored.
--no-cache Disable caching (both requests and token).
Expand Down
1 change: 1 addition & 0 deletions spotdl/console/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def entry_point():
SpotifyClient.init(
client_id=settings["client_id"],
client_secret=settings["client_secret"],
auth_token=settings["auth_token"],
user_auth=settings["user_auth"],
cache_path=settings["cache_path"],
no_cache=settings["no_cache"],
Expand Down
7 changes: 7 additions & 0 deletions spotdl/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ def parse_spotify_options(parser: _ArgumentGroup):
help="The client secret to use when logging in to Spotify.",
)

# Add auth token argument
parser.add_argument(
"--auth-token",
default=DEFAULT_CONFIG["auth_token"],
help="The authorisation token to use directly to log in to Spotify.",
)

# Add cache path argument
parser.add_argument(
"--cache-path",
Expand Down
1 change: 1 addition & 0 deletions spotdl/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def get_config() -> Dict[str, Any]:
"overwrite": "skip",
"client_id": "5f573c9620494bae87890c0f08a60293",
"client_secret": "212476d9b0f3472eaa762d90b19b0ba8",
"auth_token": None,
"user_auth": False,
"search_query": None,
"filter_results": True,
Expand Down
6 changes: 6 additions & 0 deletions spotdl/utils/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def init( # pylint: disable=bad-mcs-method-argument
self,
client_id: str,
client_secret: str,
auth_token: Optional[str] = None,
user_auth: bool = False,
cache_path: Optional[str] = None,
no_cache: bool = False,
Expand All @@ -64,6 +65,7 @@ def init( # pylint: disable=bad-mcs-method-argument
### Arguments
- client_id: The client ID of the application.
- client_secret: The client secret of the application.
- auth_token: The access token to use.
- user_auth: Whether or not to use user authentication.
- cache_path: The path to the cache file.
- no_cache: Whether or not to use the cache.
Expand Down Expand Up @@ -101,12 +103,15 @@ def init( # pylint: disable=bad-mcs-method-argument
client_secret=client_secret,
cache_handler=cache_handler,
)
if auth_token is not None:
credential_manager = None

self.user_auth = user_auth
self.no_cache = no_cache

# Create instance
self._instance = super().__call__(
auth=auth_token,
auth_manager=credential_manager,
status_forcelist=(429, 500, 502, 503, 504, 404),
)
Expand All @@ -129,6 +134,7 @@ def __init__(self, *args, **kwargs):
Initializes the SpotifyClient.
### Arguments
- auth: The access token to use.
- auth_manager: The auth manager to use.
"""

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def returncode(self):
def new_initialize(
client_id,
client_secret,
auth_token=None,
user_auth=False,
cache_path=None,
no_cache=True,
Expand All @@ -51,6 +52,7 @@ def new_initialize(
return ORIGINAL_INITIALIZE(
client_id=client_id,
client_secret=client_secret,
auth_token=auth_token,
user_auth=user_auth,
cache_path=cache_path,
no_cache=True,
Expand Down

0 comments on commit 9a69845

Please sign in to comment.