Skip to content

Instantly share code, notes, and snippets.

@chrisvanpatten
Last active November 28, 2018 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvanpatten/5d8934d74f8cbac4aade8c322c4087c5 to your computer and use it in GitHub Desktop.
Save chrisvanpatten/5d8934d74f8cbac4aade8c322c4087c5 to your computer and use it in GitHub Desktop.
var request = require('request-promise')
/**
* Spotify
*/
var base = 'https://api.spotify.com/v1/'
var spotify_uri = process.env.SPOTIFY_URI
var spotify_token = null
var spotify_refresh = process.env.SPOTIFY_REFRESH_TOKEN
var device_id = process.env.DEVICE_ID
/**
* Run the handler
*/
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2))
request.post({
url: 'https://accounts.spotify.com/api/token',
headers: {
'Authorization': 'Basic #######', // Put your encoded your Spotify credentials here
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'grant_type=refresh_token&refresh_token=' + spotify_refresh,
}).then(function (body) {
var body = JSON.parse(body)
spotify_token = body.access_token
return request.put({
url: base + 'me/player/play',
headers: {
'Authorization': 'Bearer ' + spotify_token,
},
qs: {
'device_id': device_id,
},
body: {
'context_uri': spotify_uri,
},
json: true,
})
}).catch(function (err) {
console.log(err)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment