Skip to content

Instantly share code, notes, and snippets.

@chrisvanpatten chrisvanpatten/spotify-trigger.js Secret
Last active Nov 28, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.