Skip to content

Commit 76a980d

Browse files
committed
Add Medium API example
1 parent ca930e1 commit 76a980d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

samples/Medium.gs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var CLIENT_ID;
2+
var CLIENT_SECRET;
3+
4+
/**
5+
* Authorizes and makes a request to the Medium API.
6+
*/
7+
function run() {
8+
var service = getService_();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.medium.com/v1/me';
11+
var response = UrlFetchApp.fetch(url, {
12+
headers: {
13+
Authorization: 'Bearer ' + service.getAccessToken()
14+
}
15+
});
16+
var result = JSON.parse(response.getContentText());
17+
Logger.log(JSON.stringify(result, null, 2));
18+
} else {
19+
var authorizationUrl = service.getAuthorizationUrl();
20+
Logger.log('Open the following URL and re-run the script: %s',
21+
authorizationUrl);
22+
}
23+
}
24+
25+
/**
26+
* Reset the authorization state, so that it can be re-tested.
27+
*/
28+
function reset() {
29+
var service = getService_();
30+
service.reset();
31+
}
32+
33+
34+
/**
35+
* Configures the service.
36+
* Three required parameters are not specified because
37+
* the library creates the authorization URL with them
38+
* automatically: `redirect_url`, `response_type`, and
39+
* `state`.
40+
*/
41+
function getService_() {
42+
43+
return OAuth2.createService('Medium')
44+
// Set the endpoint URLs.
45+
.setAuthorizationBaseUrl('https://medium.com/m/oauth/authorize')
46+
.setTokenUrl('https://api.medium.com/v1/tokens')
47+
48+
// Set the client ID and secret.
49+
.setClientId(CLIENT_ID)
50+
.setClientSecret(CLIENT_SECRET)
51+
52+
// Set the name of the callback function that should be invoked to complete
53+
// the OAuth flow.
54+
.setCallbackFunction('authCallback_')
55+
56+
// Set the property store where authorized tokens should be persisted.
57+
.setPropertyStore(PropertiesService.getUserProperties())
58+
59+
// Set scope (required)
60+
.setScope('basicProfile');
61+
}
62+
63+
/**
64+
* Handles the OAuth callback.
65+
*/
66+
function authCallback_(request) {
67+
var service = getService_();
68+
var authorized = service.handleCallback(request);
69+
if (authorized) {
70+
return HtmlService.createHtmlOutput('Success!');
71+
} else {
72+
return HtmlService.createHtmlOutput('Denied');
73+
}
74+
}

0 commit comments

Comments
 (0)