home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
74.16k stars 31.14k forks source link

Unable to authenticate with Spotify #52995

Closed rokam closed 3 years ago

rokam commented 3 years ago

The problem

I receive an error 500 after spotify redirect during OAUTH.

My Spotify Redirect URI is: https://my.valid.domain:8123/auth/external/callback and I receive an ERROR 500 after redirect: 500 Internal Server Error Server got itself in trouble

What is version of Home Assistant Core has the issue?

2021.7.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

Spotify

Link to integration documentation on our website

https://www.home-assistant.io/integrations/spotify

Example YAML snippet

spotify:
  client_id: !secret spotify_cid
  client_secret: !secret spotify_secret

Anything in the logs that might be useful for us?

Logger: aiohttp.server
Source: components/spotify/config_flow.py:42
First occurred: 20:19:48 (3 occurrences)
Last logged: 20:22:49

Error handling request
Traceback (most recent call last):
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/forwarded.py", line 197, in forwarded_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/request_context.py", line 24, in request_context_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/ban.py", line 78, in ban_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/auth.py", line 144, in auth_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/view.py", line 135, in handle
    result = await result
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/config/config_entries.py", line 146, in get
    return await super().get(request, flow_id)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/data_entry_flow.py", line 95, in get
    result = await self._flow_mgr.async_configure(flow_id)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/data_entry_flow.py", line 202, in async_configure
    result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/data_entry_flow.py", line 260, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/config_entry_oauth2_flow.py", line 308, in async_step_creation
    return await self.async_oauth_create_entry(
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/spotify/config_flow.py", line 42, in async_oauth_create_entry
    spotify = Spotify(auth=data["token"]["access_token"])
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/spotipy/client.py", line 166, in __init__
    self._build_session()
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/spotipy/client.py", line 193, in _build_session
    retry = urllib3.Retry(
TypeError: __init__() got an unexpected keyword argument 'allowed_methods'

Logger: aiohttp.server
Source: helpers/config_entry_oauth2_flow.py:205
First occurred: 20:19:52 (3 occurrences)
Last logged: 20:22:50

Error handling request
Traceback (most recent call last):
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/forwarded.py", line 197, in forwarded_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/request_context.py", line 24, in request_context_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/ban.py", line 78, in ban_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/auth.py", line 144, in auth_middleware
    return await handler(request)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/components/http/view.py", line 135, in handle
    result = await result
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/config_entry_oauth2_flow.py", line 421, in get
    await hass.config_entries.flow.async_configure(
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/data_entry_flow.py", line 202, in async_configure
    result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/data_entry_flow.py", line 260, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/config_entry_oauth2_flow.py", line 297, in async_step_creation
    token = await self.flow_impl.async_resolve_external_data(self.external_data)
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/config_entry_oauth2_flow.py", line 169, in async_resolve_external_data
    return await self._token_request(
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/config_entry_oauth2_flow.py", line 205, in _token_request
    resp.raise_for_status()
  File "/home/kodi/homeassistant/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1000, in raise_for_status
    raise ClientResponseError(
aiohttp.client_exceptions.ClientResponseError: 400, message='Bad Request', url=URL('https://accounts.spotify.com/api/token')

Additional information

My HASSIO is running on a py enviroment, behind a nginx proxy for SSL.

http:
  server_port: 8180
  use_x_forwarded_for: True
  trusted_proxies:
    - 127.0.0.1
    - 192.168.0.51
probot-home-assistant[bot] commented 3 years ago

Hey there @frenck, mind taking a look at this issue as its been labeled with an integration (spotify) you are listed as a codeowner for? Thanks! (message by CodeOwnersMention)

frenck commented 3 years ago

This is generally caused by a mismatch between the redirect URI you've configured in the application configuration @ the Spotify developer portal and the URL you are actually using in your browser to link the account.

Those need to match.

rokam commented 3 years ago

@frenck I'm using the same URI. I do access my HASSIO using https://my.valid.domain:8123/ so I think that the correct URI is https://my.valid.domain:8123/auth/external/callback

What am I doing wrong?

frenck commented 3 years ago
rokam commented 3 years ago

I've removed the https, now I'm accessing my instance with: http://192.168.0.51:8180/

And my spotify callback url is: image

And the OAUTH process redirects to: http://192.168.0.51:8180/auth/external/callback?code=AQDcJXQC03GDfmS98EpruWbfVSRmTuOByu49pe3bu4ZJtla35QolBMWa32TxWpYmX7g85NzKbUw5dkDW6BRR3GUpQu0D6OlHUCs8Hq8RBfsxLSKcWG_2c00YSWNTng3GPeJcdAXVMkQTvzzPRDP-jp3pjPNfCabZgPWIilXVPnu15A9uMP2wWdEyPlNDy-AXPS_6mRUspA4LjcHif4ANmqjMkuXa1_nmQyXxQNN1eR2Bv33R_-p9f4ABZUJo8aEjHQ5VZJxQdRMi9J0e1lqhj9NjXvjTYUkWwxo3YJJtAvwPXhDkOGosEMLQOLZ98aKg54rCfEzwdzcKT3cvoqImx6Cq1cWRt_8YCOFEFg8lOaHUDdBfgkvYHE7f4XTlpCSlOKTOm9tSuZd7Xzh6MzGqNXlLrb52gMuSHtQt_dOYJFYuO_ZAPurpjQNyAOSj3Y1_RWGCHF-7mcxOncxRwvHjIl5Iul34Nbl5BXp6rmxhGU5iZkfDUvMKBKSt2hc&state=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmbG93X2lkIjoiMmZlN2ZiYWM4NDg1NDRjNzk0OGQ2MTEyNjNkM2NlZGYiLCJyZWRpcmVjdF91cmkiOiJodHRwOi8vMTkyLjE2OC4wLjUxOjgxODAvYXV0aC9leHRlcm5hbC9jYWxsYmFjayJ9.RhKfV5sVCOofgV18zQgz77ECOLFnZz25mAKzzwFv020

And still I receive an error 500

rokam commented 3 years ago

I found the issue. It was related to an outdated version of urllib3 that is used by spotipy.