{"id":191,"date":"2019-03-22T23:49:54","date_gmt":"2019-03-22T15:49:54","guid":{"rendered":"https:\/\/gri.my\/blog\/?p=191"},"modified":"2019-03-22T23:49:54","modified_gmt":"2019-03-22T15:49:54","slug":"how-to-renew-spotify-api-access-token","status":"publish","type":"post","link":"https:\/\/gri.my\/blog\/how-to-renew-spotify-api-access-token\/","title":{"rendered":"How to renew Spotify API access token"},"content":{"rendered":"\n<p>There&#8217;s the official way of requesting an API token, and then there&#8217;s this method.<\/p>\n\n\n\n<p>There&#8217;s two endpoint in which you can access Spotify&#8217;s resources. There&#8217;s the typical HTML generating one (open.spotify.com) and a REST API (api.spotify.com).<\/p>\n\n\n\n<p>When you first visit the HTML endpoint, Spotify assigns you an access token in the form a cookie string, like so<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"112\" src=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-700x112.png\" alt=\"\" class=\"wp-image-192\" srcset=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-700x112.png 700w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-300x48.png 300w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-768x123.png 768w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image.png 1179w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p> This access token, can actually be used to access resources at the API endpoint, like so<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"108\" src=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-2-700x108.png\" alt=\"\" class=\"wp-image-195\" srcset=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-2-700x108.png 700w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-2-300x46.png 300w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-2-768x118.png 768w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-2.png 895w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p>The scope of the token is limited, in my experience, to public content like public playlists, search, artists, browse and tracks endpoints. You cannot use the token to enumerate, say a user&#8217;s playlist. In order to do that, you need an authenticated token, which you need to login to get. But what if you&#8217;re like me, who doesn&#8217;t want to go through the trouble of setting up the whole stupid OAuth flow and just wants things to work, you can make use of the wp_sso_token from the HTML endpoint emitted when you first login.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"94\" src=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-3-700x94.png\" alt=\"\" class=\"wp-image-196\" srcset=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-3-700x94.png 700w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-3-300x40.png 300w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-3-768x103.png 768w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-3.png 1140w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p>Using this SSO token, one can use it to request for new privileged access tokens.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"244\" src=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-4-700x244.png\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-4-700x244.png 700w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-4-300x104.png 300w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-4-768x267.png 768w, https:\/\/gri.my\/blog\/wp-content\/uploads\/2019\/03\/image-4.png 1055w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p> The SSO token, as can be seen is valid for a year. So when the access token expires after 3600 seconds, just use the SSO token to renew and you&#8217;re good to go. In my case, to renew the access token I also need to include a sp_dc token which is also emitted when you first login. The validity of the sp_dc token, thankfully, is also set to expire in a year and is interchangeable with other SSO token, meaning the two are not tied together.<\/p>\n\n\n\n<p>With this privileged access token, you can then use it to do things like, enumerate a user&#8217;s playlist, and then use that to grab the playlist&#8217;s contents. Great for scraper.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s the official way of requesting an API token, and then there&#8217;s this method. There&#8217;s two endpoint in which you can access Spotify&#8217;s resources. There&#8217;s the typical HTML generating one (open.spotify.com) and a REST API&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":2,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/191\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}