commit 684ad7dae1345ef4b68c43192a98cddd07f31cc5 Author: jperez Date: Wed May 14 10:27:27 2014 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..612c721 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Spotify Accounts Authentication Examples + +This project contains basic demos showing the different oAuth2 flows for [authenticating against the Spotify Web API](https://developer.spotify.com/spotify-web-api/authorization-guide/). + +These examples cover: + +* Authorization Code flow +* Client Credentials flow +* Implicit Grant flow + +## Installation + +These examples run on Node.js. On [its website](http://www.nodejs.org/download/) you can find instructions on how to install it. You can also follow [this gist](https://gist.github.com/isaacs/579814) for a quick and easy way to install Node.js and npm. + +Once installed, clone the repository and install its dependencies running: + + $ npm install + +## Running the examples +In order to run the different examples, open the folder with the name of the flow you want to try out, and run its `app.js` file. For instance, to run the Authorization Code example do: + + $ cd authorization_code + $ node app.js + +Then, open `http://localhost:8888` in a browser. + +### Using your own credentials +The examples contains a working client ID and secret key. Note, however, that they might be rate limited if they are used frequently. If you are planning to create an application, we recommend you register your app and get your own credentials instead of using the ones in this project. + +Go to [My Applications on Spotify Developer](https://developer.spotify.com/my-applications) and create your application. For the examples, we registered these Redirect URIs: + +* http://localhost:8888 (needed for the implicit grant flow) +* http://localhost:8888/callback + +Once you have created your app, replace the `client_id`, `redirect_uri` and `secret_key` in the examples with the ones you get from My Applications. + diff --git a/app.js b/app.js new file mode 100644 index 0000000..a4c0c18 --- /dev/null +++ b/app.js @@ -0,0 +1,6 @@ +var http = require("http"); +http.createServer(function(request, response) { + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write("Hello World"); + response.end(); +}).listen(8888); diff --git a/authorization_code/app.js b/authorization_code/app.js new file mode 100644 index 0000000..d61e5a3 --- /dev/null +++ b/authorization_code/app.js @@ -0,0 +1,102 @@ +/** + * This is an example of a basic node.js script that performs + * the Authorization Code oAuth2 flow to authenticate against + * the Spotify Accounts. + * + * For more information, read + * https://developer.spotify.com/spotify-web-api/authorization-guide/#authorization_code_flow + */ + +var express = require('express'); // Express web server framework +var request = require('request'); // "Request" library +var querystring = require('querystring'); + +var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id +var secret_key = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your secret key +var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri + +var app = express(); + +app.use(express.static(__dirname + '/public')); + +app.get('/login', function(req, res) { + + // your application requests authorization + var scope = 'user-read-private user-read-email'; + res.redirect('https://accounts.spotify.com/authorize?' + + querystring.stringify({ + response_type: 'code', + client_id: client_id, + scope: scope, + redirect_uri: redirect_uri + })); +}); + +app.get('/callback', function(req, res) { + + // your application requests refresh and access tokens + var code = req.query.code; + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + headers: { + 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64')) + }, + form: { + code: code, + redirect_uri: redirect_uri, + grant_type: 'authorization_code' + }, + json: true + }; + request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + + var access_token = body.access_token, + refresh_token = body.refresh_token; + + var options = { + url: 'https://api.spotify.com/v1/me', + headers: { 'Authorization': 'Bearer ' + access_token }, + json: true + }; + + // use the access token to access the Spotify Web API + request.get(options, function(error, response, body) { + console.log(body); + }); + + // we can also pass the token to the browser to make requests from there + res.redirect('/#' + + querystring.stringify({ + access_token: access_token, + refresh_token: refresh_token + })); + } + }); +}); + +app.get('/refresh_token', function(req, res) { + + // requesting access token from refresh token + var refresh_token = req.query.refresh_token; + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64')) }, + form: { + grant_type: 'refresh_token', + refresh_token: refresh_token + }, + json: true + }; + + request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + var access_token = body.access_token; + res.send({ + 'access_token': access_token + }); + } + }); +}); + +app.listen(8888); diff --git a/authorization_code/public/index.html b/authorization_code/public/index.html new file mode 100644 index 0000000..4d154b8 --- /dev/null +++ b/authorization_code/public/index.html @@ -0,0 +1,134 @@ + + + + Example of the Authorization Code flow with Spotify + + + + + +
+
+

This is an example of the Authorization Code flow

+ Log in with Spotify +
+
+
+
+
+
+ +
+
+ + + + + + + + + + diff --git a/client_credentials/app.js b/client_credentials/app.js new file mode 100644 index 0000000..e3e5f5b --- /dev/null +++ b/client_credentials/app.js @@ -0,0 +1,44 @@ +/** + * This is an example of a basic node.js script that performs + * the Client Credentials oAuth2 flow to authenticate against + * the Spotify Accounts. + * + * For more information, read + * https://developer.spotify.com/spotify-web-api/authorization-guide/#client_credentials_flow + */ + +var request = require('request'); // "Request" library + +var client_id = '03ffe0cac0a0401aa6673c3cf6d02ced'; // Your client id +var secret_key = 'a57c43efb9644574a96d6623fb8bfbc2'; // Your secret key +var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri + +// your application requests authorization +var authOptions = { + url: 'https://accounts.spotify.com/api/token', + headers: { + 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + secret_key).toString('base64')) + }, + form: { + grant_type: 'client_credentials' + }, + json: true +}; + +request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + + // use the access token to access the Spotify Web API + var token = body.access_token; + var options = { + url: 'https://api.spotify.com/v1/users/jmperezperez', + headers: { + 'Authorization': 'Bearer ' + token + }, + json: true + }; + request.get(options, function(error, response, body) { + console.log(body); + }); + } +}); diff --git a/implicit_grant/app.js b/implicit_grant/app.js new file mode 100644 index 0000000..9d525d9 --- /dev/null +++ b/implicit_grant/app.js @@ -0,0 +1,13 @@ +/** + * This is an example of a basic node.js script that performs + * the Implicit Grant oAuth2 flow to authenticate against + * the Spotify Accounts. + * + * For more information, read + * https://developer.spotify.com/spotify-web-api/authorization-guide/#implicit_grant_flow + */ + +var express = require('express'); // Express web server framework +var app = express(); +app.use(express.static(__dirname + '/public')); +app.listen(8888); diff --git a/implicit_grant/public/index.html b/implicit_grant/public/index.html new file mode 100644 index 0000000..54da863 --- /dev/null +++ b/implicit_grant/public/index.html @@ -0,0 +1,130 @@ + + + + Example of the Implicit Grant flow with Spotify + + + + + +
+
+

This is an example of the Implicit Grant flow

+ +
+
+
+
+
+
+
+
+ + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..7067828 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "author": "Doozer", + "name": "web-api-code-example", + "description": "Basic examples of the Spotify authorization flows through oAuth2", + "version": "0.0.1", + "main": "app.js", + "dependencies": { + "express": "~4.0.0", + "request": "~2.34.0", + "querystring": "~0.2.0" + } +}