Search code examples
sonos

Debugging Sonos error after refresh response


The Sonos Music API (SMAPI) implementation I built is working fine. I can select playlists/albums and play tracks using customsd.html. But after a couple of minutes the Sonos app fails and tells me "Trial period expired" (Sonos's Client.LoginUnauthorized error message).

I keep an error log which keeps track of all Sonos request on the server. In it, you can see that at 21:39:54 a refresh token (Client.TokenRefreshRequired) is sent back and the subsequent request uses this authentication token to authorize the getLastUpdate request. The next request fails because it uses an invalidated authorization token.

2018-12-15 21:39:19.680877:  server gets request: getMediaURI
2018-12-15 21:39:19.681059:  user agent: ["Linux UPnP\/1.0 Sonos\/47.2-59120 (ZP90)"]
2018-12-15 21:39:19.767269:  AUTHORIZE REQUEST starting with token: 26052eec7baed95cf51bb497acb74445e7f3f86c
2018-12-15 21:39:20.022666:  Successfully ended getMediaURI

2018-12-15 21:39:54.861666:  server gets request: getLastUpdate
2018-12-15 21:39:54.861842:  user agent: ["Linux UPnP\/1.0 Sonos\/47.2-59120 (WDCR:Microsoft Windows NT 10.0.17134)"]
2018-12-15 21:39:54.953047:  AUTHORIZE REQUEST starting: with token: 26052eec7baed95cf51bb497acb74445e7f3f86c
2018-12-15 21:39:55.177064:  Sending refreshed access token. token 93467644b3c00ec738a06f41abb44f5925977da6

2018-12-15 21:39:55.997627:  server gets request: getLastUpdate
2018-12-15 21:39:55.998382:  user agent: ["Linux UPnP\/1.0 Sonos\/47.2-59120 (WDCR:Microsoft Windows NT 10.0.17134)"]
2018-12-15 21:39:56.111795:  AUTHORIZE REQUEST starting with token: 93467644b3c00ec738a06f41abb44f5925977da6
2018-12-15 21:39:56.338871:  Successfully ended getLastUpdate

2018-12-15 21:40:10.834301:  server gets request: getMediaURI
2018-12-15 21:40:10.834675:  user agent: Linux UPnP\/1.0 Sonos\/47.2-59120 (ZP90)
2018-12-15 21:40:10.928440:  AUTHORIZE REQUEST starting with token: 26052eec7baed95cf51bb497acb74445e7f3f86c
2018-12-15 21:40:10.940039:  FAILED AUTHENTICATION

The authentication fails when called at 21:40:10. This call is made by user agent ending with (ZP90), which I presume is my physical Sonos player. The refreshed acces token is sent to the user agent ending with (WDCR:Microsoft Windows NT 10.0.17134); presumably my desktop Sonos app?.

Is it normal that two different user agents call the service? if so, how to deal with expiring authorization tokens? Should there be a delay in expiring them? if so, how long should this delay be?

EDIT: My flow is:

  1. A request is sent with token A by the Controller.
  2. My authorization server says it has expired, revokes token A and sends back the new token B.
  3. The Controller sends requests with token B, all fine!
  4. Next thing that happens: the Player sends a request with token A, which has been revoked, so we are forced to send back an unauthorized token error.
  5. The process gets stuck because the Player keeps requesting with token A. Error! We cannot refresh a revoked token!

How could I continue with revoked authorization tokens?


Solution

  • The player executes its own token refresh flow. As long as you correctly handle the refresh flow on your end, things will work out. If you notice an expired token, simply refresh it, regardless of client.

    As an aside, if the player does get a refresh token, it will pass it up to the controller. The opposite does not happen (as you noticed).