Fabrizz / MMM-OnSpotify

Highly customizable MM2 module that displays what you are listening to in Spotify. Compatible with MMM-LiveLyrics and DynamicTheming.
MIT License
65 stars 10 forks source link

Nothing is playing #50

Closed bird-dog closed 9 months ago

bird-dog commented 9 months ago

Hey there, I am on the latest version and the module shows my account picture, subscription (premium) and a bunch of album art from artists I have been listening to, but when I play Spotify on my ipad, iphone, or mac, it does not change

I checked the logs from pm2, but I do not see any errors.

Where else should I try to diagnose?

I also noticed that the auth server steps are out of date as you need to set a redirect url in order to save the spotify app. I could not find any information on what to set that to, but I appended /callback and it just so happened to work.

Fabrizz commented 9 months ago

Hi, you should check errors on the PM2 process for MM or the electron webview (with Ctrl+Shift+I or F12), if there is an error getting your player data, there should be an error there.

About redirect uri, the last step on the Auth server is:

Click on “Edit Settings” and add http://localhost:8100/callback to the Redirect URIs and save your settings. (You need to click Add and then Save

Sometimes old MM2 installs break the module, I still cant figure why. Are you using any theming or module config? I would test without them or the spelling

bird-dog commented 9 months ago

I see these errors in ~/.pm2/logs/mm-error.log:

[19.02.2024 12:39.07.614] [ERROR] [MMM-NPOS] [Node Helper] Get player Data >>  FetchError: request to https://api.spotify.com/v1/me/player?additional_types=track,episode failed, reason: getaddrinfo EAI_AGAIN api.spotify.com
    at ClientRequest.<anonymous> (/home/joel/MagicMirror/modules/MMM-OnSpotify/node_modules/node-fetch/lib/index.js:1501:11)
    at ClientRequest.emit (node:events:514:28)
    at TLSSocket.socketErrorListener (node:_http_client:501:9)
    at TLSSocket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'EAI_AGAIN',
  code: 'EAI_AGAIN'
}
[19.02.2024 12:39.11.635] [ERROR] [MMM-NPOS] [Node Helper] Get player Data >>  FetchError: request to https://api.spotify.com/v1/me/player?additional_types=track,episode failed, reason: getaddrinfo EAI_AGAIN api.spotify.com
    at ClientRequest.<anonymous> (/home/joel/MagicMirror/modules/MMM-OnSpotify/node_modules/node-fetch/lib/index.js:1501:11)
    at ClientRequest.emit (node:events:514:28)
    at TLSSocket.socketErrorListener (node:_http_client:501:9)
    at TLSSocket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'EAI_AGAIN',
  code: 'EAI_AGAIN'
}

But when I restart mm, i do not see them repeated.

Here is the magicmirror version I get from the logs:

[19.02.2024 12:42.12.394] [LOG] Starting MagicMirror: v2.26.0

Fabrizz commented 9 months ago

Okay that is strange, getaddrinfo means that there is a DNS lookup error EAI_AGAIN is the code for a temporary failure in name resolution (It cant resolve what api.spotify.com points to), its out of the scope of the module, but still strange as you say that its intermittent.

Are you using a proxy or similar?, is the MM2 or node install old? I would suggest updating npm (view below)

https://www.codingdefined.com/2015/06/nodejs-error-errno-eaiagain.html

image
Fabrizz commented 9 months ago

Are you using something like PiHole or similar adblockers / custom dns?

bird-dog commented 9 months ago

Looks like that EAI_AGAIN error only occurs when mm starts up, but my raspberry pi is not connected to wifi.

I am not using a proxy. As far as I know the 2.26 version of mm2 is the newest. I tried updating npm, but all was up to date

npm version
{
  npm: '8.19.2',
  node: '18.12.0',
  v8: '10.2.154.15-node.12',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '108',
  nghttp2: '1.47.0',
  napi: '8',
  llhttp: '6.0.10',
  openssl: '3.0.5+quic',
  cldr: '41.0',
  icu: '71.1',
  tz: '2022b',
  unicode: '14.0',
  ngtcp2: '0.8.1',
  nghttp3: '0.7.0'
}

No adblockers or dns changes. This is a freshly installed raspberrypi with just MM2 installed so far

Fabrizz commented 9 months ago

After internet is connected it still does not work right? Other modules / MM2 can connect without problem?

bird-dog commented 9 months ago

It works, but only partly. It is showing my actual account name and the grid with 9 albums are all albums I have recently listened to, but it never shows that I am playing a song from another device. It shows "Nothing is playing"

bird-dog commented 9 months ago

The other default MM modules are updating correctly

bird-dog commented 9 months ago

I am wondering if I did not setup my auth correctly. Getting this when doing a curl

curl https://api.spotify.com/v1/me/player?additional_types=track,episode -H "Authorization: Bearer <accessToken from MagicMirror/config/config.json>"
{
  "error": {
    "status": 401,
    "message": "The access token expired"
  }
}

Does the auth server this module starts need to be left running?

Fabrizz commented 9 months ago

Does the auth server this module starts need to be left running?

No, its just a small app to get the token. Maybe trying to redo the auth again solves it?

To test the credentials you should do:

curl -X POST "https://accounts.spotify.com/api/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=your-client-id&client_secret=your-client-secret"

I cant reproduce the issue. It still does not make sense as you can query your own account ("personal data", the albums are shown)

Fabrizz commented 9 months ago

Then with that bearer token you can query /v1/me/player to test if it works

Fabrizz commented 9 months ago

Can you try something dumb? After all modules are loaded refresh the page with F5, that should try to redo the token auth

bird-dog commented 9 months ago

The response I get from the token request looks like this

{"access_token":"TOKEN","token_type":"Bearer","expires_in":3600}

should it not list the scopes?

Fabrizz commented 9 months ago

Its okay, the scopes are included only when you use the refresh token. Using that bearer to get the player endpoint works?

Fabrizz commented 9 months ago

In your config you have the four credentials right?

Fabrizz commented 9 months ago
{
    module: "MMM-OnSpotify",
    position: "bottom_right", /* bottom_left, bottom_center */
    disabled: false,
    config: {
        clientID: "<clientID>",
        clientSecret: "<clientSecret>",
        accessToken: "<accessToken>",
        refreshToken: "<refreshToken>",
    }
},

Edit:

"Authorization: Bearer <accessToken from MagicMirror/config/config.json>"

You have them

bird-dog commented 9 months ago

I get the token fine, but then I get

curl https://api.spotify.com/v1/me/player -H "Authorization: Bearer TOKEN"
{
  "error" : {
    "status" : 404,
    "message" : "Invalid username"
  }
}

Does this have anything to do with the fact that I have a very old spotify account? It was created with Facebook when that was the only option to create an account.

bird-dog commented 9 months ago

Yep I have the 4 config settings

bird-dog commented 9 months ago

Oh wait using the bearer token from the config.js does work

curl https://api.spotify.com/v1/me/player -H "Authorization: Bearer <accessToken from MagicMirror/config/config.json>"
{
  "device" : {
    "id" : "ID",
    "is_active" : true,
    "is_private_session" : false,
    "is_restricted" : false,
    "name" : "iPad",
    "supports_volume" : false,
    "type" : "Tablet",
    "volume_percent" : 100
  },
  "shuffle_state" : false,
  "smart_shuffle" : false,
  "repeat_state" : "off",
...
Fabrizz commented 9 months ago

Sorry, you should use the access token. Just seen the edit

Fabrizz commented 9 months ago

Let me try a couple of things, as I cannot seem reproduce the issue

bird-dog commented 9 months ago

Does the module log to stdout? I see successful log entries for the other apps, but not this one

Fabrizz commented 9 months ago

Yes, with pm2 is pm2 logs if I remember correctly. You have the access token that you generated right? Can you put it here just to see if it works:

  /* utils/SpotifyFetcher.js L42*/
  requestData(type) {
    let sl = "v1/me/top/artists?limit=9";
    const headers = new Headers();
    headers.append("Authorization", `Bearer ${this.credentials.accessToken}`); /* `Bearer <your token>` */

Just to try

bird-dog commented 9 months ago

Now it just says "connecting to spotify..." Here is the json output I get

curl https://api.spotify.com/v1/me/player?additional_types=track,episode -H "Authorization: Bearer <TOKEN>" -H "Accept-Language: en-US"
{
  "device" : {
    "id" : "<id>",
    "is_active" : true,
    "is_private_session" : false,
    "is_restricted" : false,
    "name" : "iPad",
    "supports_volume" : false,
    "type" : "Tablet",
    "volume_percent" : 100
  },
  "shuffle_state" : false,
  "smart_shuffle" : false,
  "repeat_state" : "off",
  "timestamp" : 1708381002350,
  "context" : {
    "external_urls" : {
      "spotify" : "https://open.spotify.com/album/0RjtEnaoDqLhxCRc3i9q3J"
    },
    "href" : "https://api.spotify.com/v1/albums/0RjtEnaoDqLhxCRc3i9q3J",
    "type" : "album",
    "uri" : "spotify:album:0RjtEnaoDqLhxCRc3i9q3J"
  },
  "progress_ms" : 167167,
  "item" : {
    "album" : {
      "album_type" : "album",
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/4UjrcYERvCDC44O0uBSOFa"
        },
        "href" : "https://api.spotify.com/v1/artists/4UjrcYERvCDC44O0uBSOFa",
        "id" : "4UjrcYERvCDC44O0uBSOFa",
        "name" : "Gleemer",
        "type" : "artist",
        "uri" : "spotify:artist:4UjrcYERvCDC44O0uBSOFa"
      } ],
      "available_markets" : [ "AR", "AU", "AT", "BE", "BO", "BR", "BG", "CA", "CL", "CO", "CR", "CY", "CZ", "DK", "DO", "DE", "EC", "EE", "SV", "FI", "FR", "GR", "GT", "HN", "HK", "HU", "IS", "IE", "IT", "LV", "LT", "LU", "MY", "MT", "MX", "NL", "NZ", "NI", "NO", "PA", "PY", "PE", "PH", "PL", "PT", "SG", "SK", "ES", "SE", "CH", "TW", "TR", "UY", "US", "GB", "AD", "LI", "MC", "ID", "JP", "TH", "VN", "RO", "IL", "ZA", "SA", "AE", "BH", "QA", "OM", "KW", "EG", "MA", "DZ", "TN", "LB", "JO", "PS", "IN", "BY", "KZ", "MD", "UA", "AL", "BA", "HR", "ME", "MK", "RS", "SI", "KR", "BD", "PK", "LK", "GH", "KE", "NG", "TZ", "UG", "AG", "AM", "BS", "BB", "BZ", "BT", "BW", "BF", "CV", "CW", "DM", "FJ", "GM", "GE", "GD", "GW", "GY", "HT", "JM", "KI", "LS", "LR", "MW", "MV", "ML", "MH", "FM", "NA", "NR", "NE", "PW", "PG", "WS", "SM", "ST", "SN", "SC", "SL", "SB", "KN", "LC", "VC", "SR", "TL", "TO", "TT", "TV", "VU", "AZ", "BN", "BI", "KH", "CM", "TD", "KM", "GQ", "SZ", "GA", "GN", "KG", "LA", "MO", "MR", "MN", "NP", "RW", "TG", "UZ", "ZW", "BJ", "MG", "MU", "MZ", "AO", "CI", "DJ", "ZM", "CD", "CG", "IQ", "LY", "TJ", "VE", "ET", "XK" ],
      "external_urls" : {
        "spotify" : "https://open.spotify.com/album/0RjtEnaoDqLhxCRc3i9q3J"
      },
      "href" : "https://api.spotify.com/v1/albums/0RjtEnaoDqLhxCRc3i9q3J",
      "id" : "0RjtEnaoDqLhxCRc3i9q3J",
      "images" : [ {
        "height" : 640,
        "url" : "https://i.scdn.co/image/ab67616d0000b273a406659e40d238040a15ba54",
        "width" : 640
      }, {
        "height" : 300,
        "url" : "https://i.scdn.co/image/ab67616d00001e02a406659e40d238040a15ba54",
        "width" : 300
      }, {
        "height" : 64,
        "url" : "https://i.scdn.co/image/ab67616d00004851a406659e40d238040a15ba54",
        "width" : 64
      } ],
      "name" : "Down Through",
      "release_date" : "2020-05-08",
      "release_date_precision" : "day",
      "total_tracks" : 9,
      "type" : "album",
      "uri" : "spotify:album:0RjtEnaoDqLhxCRc3i9q3J"
    },
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/4UjrcYERvCDC44O0uBSOFa"
      },
      "href" : "https://api.spotify.com/v1/artists/4UjrcYERvCDC44O0uBSOFa",
      "id" : "4UjrcYERvCDC44O0uBSOFa",
      "name" : "Gleemer",
      "type" : "artist",
      "uri" : "spotify:artist:4UjrcYERvCDC44O0uBSOFa"
    } ],
    "available_markets" : [ "AR", "AU", "AT", "BE", "BO", "BR", "BG", "CA", "CL", "CO", "CR", "CY", "CZ", "DK", "DO", "DE", "EC", "EE", "SV", "FI", "FR", "GR", "GT", "HN", "HK", "HU", "IS", "IE", "IT", "LV", "LT", "LU", "MY", "MT", "MX", "NL", "NZ", "NI", "NO", "PA", "PY", "PE", "PH", "PL", "PT", "SG", "SK", "ES", "SE", "CH", "TW", "TR", "UY", "US", "GB", "AD", "LI", "MC", "ID", "JP", "TH", "VN", "RO", "IL", "ZA", "SA", "AE", "BH", "QA", "OM", "KW", "EG", "MA", "DZ", "TN", "LB", "JO", "PS", "IN", "BY", "KZ", "MD", "UA", "AL", "BA", "HR", "ME", "MK", "RS", "SI", "KR", "BD", "PK", "LK", "GH", "KE", "NG", "TZ", "UG", "AG", "AM", "BS", "BB", "BZ", "BT", "BW", "BF", "CV", "CW", "DM", "FJ", "GM", "GE", "GD", "GW", "GY", "HT", "JM", "KI", "LS", "LR", "MW", "MV", "ML", "MH", "FM", "NA", "NR", "NE", "PW", "PG", "WS", "SM", "ST", "SN", "SC", "SL", "SB", "KN", "LC", "VC", "SR", "TL", "TO", "TT", "TV", "VU", "AZ", "BN", "BI", "KH", "CM", "TD", "KM", "GQ", "SZ", "GA", "GN", "KG", "LA", "MO", "MR", "MN", "NP", "RW", "TG", "UZ", "ZW", "BJ", "MG", "MU", "MZ", "AO", "CI", "DJ", "ZM", "CD", "CG", "IQ", "LY", "TJ", "VE", "ET", "XK" ],
    "disc_number" : 1,
    "duration_ms" : 242724,
    "explicit" : false,
    "external_ids" : {
      "isrc" : "QM4TW2095086"
    },
    "external_urls" : {
      "spotify" : "https://open.spotify.com/track/5zFIhC4OFjO3lpSQW9zDdk"
    },
    "href" : "https://api.spotify.com/v1/tracks/5zFIhC4OFjO3lpSQW9zDdk",
    "id" : "5zFIhC4OFjO3lpSQW9zDdk",
    "is_local" : false,
    "name" : "Casino",
    "popularity" : 23,
    "preview_url" : "https://p.scdn.co/mp3-preview/2a8eaad7d3883f90eb1dc884832147bd0a058425?cid=5008c3cc62ec48fdabef29f9a9b58a6d",
    "track_number" : 7,
    "type" : "track",
    "uri" : "spotify:track:5zFIhC4OFjO3lpSQW9zDdk"
  },
  "currently_playing_type" : "track",
  "actions" : {
    "disallows" : {
      "resuming" : true
    }
  },
  "is_playing" : true
}
Fabrizz commented 9 months ago

Hey, have you managed to get it working? Its really strange. I will add some other checks/log options on the next update, but the issue is still strange.

Did this fix! Working now! #27 Could be connected