|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Example of the Authorization Code flow with Spotify</title> |
| 5 | + <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> |
| 6 | + <style type="text/css"> |
| 7 | + #login, #loggedin { |
| 8 | + display: none; |
| 9 | + } |
| 10 | + .text-overflow { |
| 11 | + overflow: hidden; |
| 12 | + text-overflow: ellipsis; |
| 13 | + white-space: nowrap; |
| 14 | + width: 500px; |
| 15 | + } |
| 16 | + </style> |
| 17 | + </head> |
| 18 | + |
| 19 | + <body> |
| 20 | + <div class="container"> |
| 21 | + <div id="login"> |
| 22 | + <h1>This is an example of the Authorization Code flow</h1> |
| 23 | + <a href="/login" class="btn btn-primary">Log in with Spotify</a> |
| 24 | + </div> |
| 25 | + <div id="loggedin"> |
| 26 | + <div id="user-profile"> |
| 27 | + </div> |
| 28 | + <div id="oauth"> |
| 29 | + </div> |
| 30 | + <button class="btn btn-default" id="obtain-new-token">Obtain new token using the refresh token</button> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + |
| 34 | + <script id="user-profile-template" type="text/x-handlebars-template"> |
| 35 | + <h1>Logged in as {{display_name}}</h1> |
| 36 | + <div class="media"> |
| 37 | + <div class="pull-left"> |
| 38 | + <img class="media-object" width="150" src="{{image.url}}" /> |
| 39 | + </div> |
| 40 | + <div class="media-body"> |
| 41 | + <dl class="dl-horizontal"> |
| 42 | + <dt>Display name</dt><dd>{{display_name}}</dd> |
| 43 | + <dt>Id</dt><dd>{{id}}</dd> |
| 44 | + <dt>Email</dt><dd>{{email}}</dd> |
| 45 | + <dt>Spotify URI</dt><dd><a href="{{self.uri}}">{{self.uri}}</a></dd> |
| 46 | + <dt>Link</dt><dd><a href="{{self.web}}">{{self.web}}</a></dd> |
| 47 | + <dt>Profile Image</dt><dd><a href="{{image.url}}">{{image.url}}</a></dd> |
| 48 | + </dl> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </script> |
| 52 | + |
| 53 | + <script id="oauth-template" type="text/x-handlebars-template"> |
| 54 | + <h2>oAuth info</h2> |
| 55 | + <dl class="dl-horizontal"> |
| 56 | + <dt>Access token</dt><dd class="text-overflow">{{access_token}}</dd> |
| 57 | + <dt>Refresh token</dt><dd class="text-overflow">{{refresh_token}}></dd> |
| 58 | + </dl> |
| 59 | + </script> |
| 60 | + |
| 61 | + <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.1/handlebars.min.js"></script> |
| 62 | + <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> |
| 63 | + <script> |
| 64 | + (function() { |
| 65 | + |
| 66 | + /** |
| 67 | + * Obtains parameters from the hash of the URL |
| 68 | + * @return Object |
| 69 | + */ |
| 70 | + function getHashParams() { |
| 71 | + var hashParams = {}; |
| 72 | + var e, r = /([^&;=]+)=?([^&;]*)/g, |
| 73 | + q = window.location.hash.substring(1); |
| 74 | + while ( e = r.exec(q)) { |
| 75 | + hashParams[e[1]] = decodeURIComponent(e[2]); |
| 76 | + } |
| 77 | + return hashParams; |
| 78 | + } |
| 79 | + |
| 80 | + var userProfileSource = document.getElementById('user-profile-template').innerHTML, |
| 81 | + userProfileTemplate = Handlebars.compile(userProfileSource), |
| 82 | + userProfilePlaceholder = document.getElementById('user-profile'); |
| 83 | + |
| 84 | + oauthSource = document.getElementById('oauth-template').innerHTML, |
| 85 | + oauthTemplate = Handlebars.compile(oauthSource), |
| 86 | + oauthPlaceholder = document.getElementById('oauth'); |
| 87 | + |
| 88 | + var params = getHashParams(); |
| 89 | + |
| 90 | + var access_token = params.access_token |
| 91 | + refresh_token = params.refresh_token; |
| 92 | + |
| 93 | + oauthPlaceholder.innerHTML = oauthTemplate({ |
| 94 | + access_token: access_token, |
| 95 | + refresh_token: refresh_token |
| 96 | + }); |
| 97 | + |
| 98 | + if (access_token) { |
| 99 | + $.ajax({ |
| 100 | + url: 'https://api.spotify.com/v1/me', |
| 101 | + headers: { |
| 102 | + 'Authorization': 'Bearer ' + access_token |
| 103 | + }, |
| 104 | + success: function(response) { |
| 105 | + userProfilePlaceholder.innerHTML = userProfileTemplate(response); |
| 106 | + |
| 107 | + $('#login').hide(); |
| 108 | + $('#loggedin').show(); |
| 109 | + } |
| 110 | + }); |
| 111 | + } else { |
| 112 | + $('#login').show(); |
| 113 | + $('#loggedin').hide(); |
| 114 | + } |
| 115 | + |
| 116 | + document.getElementById('obtain-new-token').addEventListener('click', function() { |
| 117 | + $.ajax({ |
| 118 | + url: '/refresh_token', |
| 119 | + data: { |
| 120 | + 'refresh_token': refresh_token |
| 121 | + } |
| 122 | + }).done(function(data) { |
| 123 | + access_token = data.access_token; |
| 124 | + oauthPlaceholder.innerHTML = oauthTemplate({ |
| 125 | + access_token: access_token, |
| 126 | + refresh_token: refresh_token |
| 127 | + }); |
| 128 | + }); |
| 129 | + }, false); |
| 130 | + |
| 131 | + })(); |
| 132 | + </script> |
| 133 | +</html> |
| 134 | + |
0 commit comments