node.js - Spotify API Authorization for cron job -
i'm creating node.js application update playlists (owned account in have credentials) daily. according spotify documentation, add tracks playlist (https://developer.spotify.com/web-api/add-tracks-to-playlist/), authorization must supplied using oauth2.
i'm struggling find way server side no redirects/etc. seems if can refresh token, can use that?
i've been looking @ spotify web api node module (https://github.com/thelinmichael/spotify-web-api-node), oauth.io, , spotify api.
any ideas appreciated! there 1 account have authenticated, hard-coded @ least now.
you've picked correct authorization flow - authorization code, since need access token that's connected user owns playlists you're updating. of course gives ability refresh token whenever need to. (the expiration time 1 hour, don't need refresh access token until application needs use it.)
as sidenote, client credentials flow meant server server communication doesn't require user's permission, e.g. search, read playlist, or retrieve new releases. implicit grant flow meant used in frontends, , doesn't allow refresh token.
i'm struggling find way server side no redirects/etc. seems if can refresh token, can use that?
once have refresh token can continue use retrieve new access tokens, can done without user interaction. need preparation work retrieve refresh token though.
following steps describing authorization code flow, first need direct playlist's owner url on spotify's account server.
the documentation contains following example url:
simply replace client_id
, redirect_uri
application's information. modify scope
parameter match scopes need, understanding of use case playlist-read-private,playlist-modify-private,playlist-read-collaborative
since want able read , modify of user's playlists. supplying state
not required.
using spotify-web-api-node can generate url using createauthorizeurl method, since you're doing once it's unnecessary write code it.
instead, open url in browser.
if done successfully, you'll taken through little login dance application asks permission read , modify playlists. when completed, spotify's account service redirect browser redirect_uri
url code
query parameter included described in step 3 in authorization guide.
however, since you're doing once, enough start webserver on own machine, set application's redirect_uri
localhost, , complete login flow. have @ web-api-auth-examples ready-made node.js application fires express server , reads authorization code.
once you've got code, can trade access token using curl it's done in step #4 in authorization guide, or use code in web-api-auth-examples repository.
finally, tokens retrieved (step #5), can start use web api access token, , new 1 when expires using request in step #7.
spotify-web-api-node has helper method refresh token. search main documentation refreshaccesstoken
method.
Comments
Post a Comment