-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathTwitter.gs
72 lines (62 loc) · 2.32 KB
/
Twitter.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const CLIENT_ID = '...';
const CLIENT_SECRET = '...';
function generateCodeVerifier() {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
let verifier = "";
for (let i = 0; i < 32; ++i) {
const r = Math.floor(Math.random() * charset.length);
verifier += charset[r];
}
return verifier;
}
function encodeChallenge(codeVerifier) {
const hashedValue = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, codeVerifier, Utilities.Charset.US_ASCII);
let encodedValue = Utilities.base64EncodeWebSafe(hashedValue);
encodedValue = encodedValue.slice(0, encodedValue.indexOf('=')); // Strip padding
return encodedValue;
}
/**
* Configures the service.
*/
const getService = () => {
return OAuth2.createService('Twitter')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('https://twitter.com/i/oauth2/authorize')
.setTokenUrl('https://api.twitter.com/2/oauth2/token')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Twitter services).
.setScope('users.read tweet.read follows.write like.write offline.access')
.setTokenHeaders({
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET),
'Content-Type': 'application/x-www-form-urlencoded'
})
}
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
const service = getService();
service.setTokenPayloadHandler(payload => {
payload['code_verifier'] = request.parameter.codeVerifier;
return payload;
});
const authorized = service.handleCallback(request);
if (authorized) {
const configCompleteRedirectUrl = service.getStorage().getValue('configCompleteRedirectUrl');
return HtmlService
.createHtmlOutput(`<html><body><button onclick="window.open('${configCompleteRedirectUrl}');">Finish Setup</button></body></html>`);
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
/**
* Resets OAuth config.
*/
const reset = () => getService().reset();