Skip to content

Commit cfb2748

Browse files
authored
Create Spotify.gs (#245)
* Create Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs * Update Spotify.gs
1 parent 56accb0 commit cfb2748

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

samples/Spotify.gs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This sample demonstrates how to configure the library for the Spotify API.
3+
* Instructions on how to generate OAuth credentuals is available here:
4+
* https://developer.spotify.com/documentation/general/guides/authorization-guide/
5+
*/
6+
7+
var CLIENT_ID = '...';
8+
var CLIENT_SECRET = '...';
9+
10+
/**
11+
* Authorizes and makes a request to the Spotify API.
12+
*/
13+
function run() {
14+
var service = getService();
15+
if (service.hasAccess()) {
16+
var url = 'https://api.spotify.com/v1/me';
17+
var response = UrlFetchApp.fetch(url, {
18+
headers: {'Authorization': 'Bearer ' + service.getAccessToken()}
19+
});
20+
var result = JSON.parse(response.getContentText());
21+
Logger.log(JSON.stringify(result, null, 2));
22+
} else {
23+
var authorizationUrl = service.getAuthorizationUrl();
24+
Logger.log('Open the following URL and re-run the script: %s',
25+
authorizationUrl);
26+
}
27+
}
28+
29+
/**
30+
* Reset the authorization state, so that it can be re-tested.
31+
*/
32+
function reset() {
33+
getService().reset();
34+
}
35+
36+
/**
37+
* Configures the service.
38+
*/
39+
function getService() {
40+
return OAuth2.createService('Spotify')
41+
// Set the endpoint URLs.
42+
.setAuthorizationBaseUrl('https://accounts.spotify.com/authorize')
43+
.setTokenUrl('https://accounts.spotify.com/api/token')
44+
45+
// Set the client ID and secret.
46+
.setClientId(CLIENT_ID)
47+
.setClientSecret(CLIENT_SECRET)
48+
49+
// Set the name of the callback function that should be invoked to complete
50+
// the OAuth flow.
51+
.setCallbackFunction('authCallback')
52+
53+
// Set the property store where authorized tokens should be persisted.
54+
.setPropertyStore(PropertiesService.getUserProperties());
55+
}
56+
57+
/**
58+
* Handles the OAuth callback.
59+
*/
60+
function authCallback(request) {
61+
var service = getService();
62+
var authorized = service.handleCallback(request);
63+
if (authorized) {
64+
return HtmlService.createHtmlOutput('Success!');
65+
} else {
66+
return HtmlService.createHtmlOutput('Denied.');
67+
}
68+
}
69+
70+
/**
71+
* Logs the redict URI to register.
72+
*/
73+
function logRedirectUri() {
74+
Logger.log(OAuth2.getRedirectUri());
75+
}

0 commit comments

Comments
 (0)