-
-
Save chrisvanpatten/5d8934d74f8cbac4aade8c322c4087c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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