skuethe / MMM-Spotify

Spotify display and controller for MagicMirror
MIT License
68 stars 17 forks source link

429 Too many requests #172

Closed a-waider closed 2 years ago

a-waider commented 3 years ago

I noticed that Spotify has a rate limiting feature. If there are too many requests to the Spotify App it will prevent you from sending further requests.

The error message looks something like this:

response: {
    status: 429,
    statusText: 'Too Many Requests',
    headers: {
      'cache-control': 'private, max-age=0',
      'retry-after': '19056', // This is important
      'access-control-allow-origin': '*',
      'access-control-allow-headers': 'Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token',
      'access-control-allow-methods': 'GET, POST, OPTIONS, PUT, DELETE, PATCH',
      'access-control-allow-credentials': 'true',
      'access-control-max-age': '604800',
      'content-length': '17',
      'strict-transport-security': 'max-age=31536000',
      'x-content-type-options': 'nosniff',
      date: 'Thu, 02 Sep 2021 09:48:43 GMT',
      server: 'envoy',
      via: 'HTTP/2 edgeproxy, 1.1 google',
      'alt-svc': 'clear',
      connection: 'close'
    },
    config: {
      url: '/v1/me/player',
      method: 'get',
      data: null,
      headers: [Object],
      params: [Object],
      baseURL: 'https://api.spotify.com',
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      adapter: [Function: httpAdapter],
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus]
    },
    request: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      connection: [TLSSocket],
      _header: 'GET /v1/me/player?additional_types=episode,track HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Authorization: Bearer *****' +
        'User-Agent: axios/0.21.1\r\n' +
        'Host: api.spotify.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      insecureHTTPParser: undefined,
      path: '/v1/me/player?additional_types=episode,track',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'api.spotify.com',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: 'Too many requests'
  },

Important is the "retry-after" header. It would be a nice improvement, if the Spotify client could catch the 429 HTTP error and wait the expected seconds (plus a delta) before trying again.

I'm not quite sure when the limits of the Spotify API are exceeded. Maybe anyone has some information on this.

skuethe commented 3 years ago

That is a very good idea - thanks. I am also not sure what the api rate limits are actually set to. It seems they don't communicate that publicly.

skuethe commented 3 years ago

As an addition: any particular reason why you might have triggered that limit? Multiple instances / devices or other hardware / applications in use which are also using Spotify API? I have not yet come across that problem myself, but I do remember that some other user had this problem once.

a-waider commented 3 years ago

The cause may be that I was running Home Assistant and Magic Mirror on the same Spotify App. So both together were generating double the amount of requests as of only one client.

Not sure if this is still up to date but here someone said there is an limit at 2000 requests per app per hour. In case it is still correct than 1 call per second is exceeding this limit.

a-waider commented 3 years ago

Maybe you could answer me that, cause you're way deeper in the Magic Mirror ecosystem. When I have multiple Browser tabs open, does that meen that every tab is making its own request? Or does the server make one requests and forwards the response to all open browser tabs?

skuethe commented 3 years ago

Okay, thank you for the insights. That does make sense that two apps are then triggering the limits.

About your question. That's a tough one - I am not that deep into the inside mechanics of MM². Some information I know:

As I said, this is not a really substantiated statement from my side.