Skip to content

Commit 245fc4e

Browse files
authored
Create ChatWork.gs
1 parent 2bcdd9e commit 245fc4e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

samples/ChatWork.gs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Harvest API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var response = UrlFetchApp.fetch('https://api.chatwork.com/v2/me', {
11+
headers: {
12+
Authorization: 'Bearer ' + chatWorkService.getAccessToken()
13+
}
14+
});
15+
var result = JSON.parse(response.getContentText());
16+
Logger.log(JSON.stringify(result, null, 2));
17+
} else {
18+
var authorizationUrl = service.getAuthorizationUrl();
19+
Logger.log('Open the following URL and re-run the script: %s',
20+
authorizationUrl);
21+
}
22+
}
23+
24+
/**
25+
* Reset the authorization state, so that it can be re-tested.
26+
*/
27+
function reset() {
28+
var service = getService();
29+
service.reset();
30+
}
31+
32+
/**
33+
* Configures the service.
34+
*/
35+
function getService() {
36+
var scope = 'users.profile.me:read rooms.messages:read';
37+
return OAuth2.createService('ChatWork')
38+
// Set the endpoint URLs.
39+
.setAuthorizationBaseUrl('https://www.chatwork.com/packages/oauth2/login.php')
40+
.setTokenUrl('https://oauth.chatwork.com/token')
41+
42+
// Set the client ID and secret.
43+
.setClientId(CLIENT_ID)
44+
.setClientSecret(CLIENT_SECRET)
45+
46+
// Set the name of the callback function that should be invoked to
47+
// complete the OAuth flow.
48+
.setCallbackFunction('authCallback')
49+
50+
.setScope(scope)
51+
52+
.setTokenHeaders({
53+
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
54+
})
55+
.setTokenPayloadHandler(function(tokenPayload) {
56+
delete tokenPayload.client_id;
57+
return tokenPayload;
58+
})
59+
// Set the property store where authorized tokens should be persisted.
60+
.setPropertyStore(PropertiesService.getUserProperties())
61+
;
62+
}
63+
64+
/**
65+
* Handles the OAuth callback.
66+
*/
67+
function authCallback(request) {
68+
var service = getService();
69+
var authorized = service.handleCallback(request);
70+
if (authorized) {
71+
return HtmlService.createHtmlOutput('Success!');
72+
} else {
73+
return HtmlService.createHtmlOutput('Denied.');
74+
}
75+
}
76+
77+
/**
78+
* Logs the redict URI to register.
79+
*/
80+
function logRedirectUri() {
81+
var service = getService();
82+
Logger.log(service.getRedirectUri());
83+
}

0 commit comments

Comments
 (0)