Skip to content

Commit 7c171db

Browse files
authored
Update ChatWork.gs
1 parent deea2a0 commit 7c171db

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

samples/ChatWork.gs

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,84 @@ var CLIENT_ID = '...';
22
var CLIENT_SECRET = '...';
33

44
/**
5-
* Authorizes and makes a request to the Harvest API.
5+
* Authorizes and makes a request to the ChatWork API.
66
*/
77
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 ' + service.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-
}
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var response = UrlFetchApp.fetch('https://api.chatwork.com/v2/me', {
11+
headers: {
12+
Authorization: 'Bearer ' + service.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+
}
2222
}
2323

2424
/**
2525
* Reset the authorization state, so that it can be re-tested.
2626
*/
2727
function reset() {
28-
var service = getService();
29-
service.reset();
28+
var service = getService();
29+
service.reset();
3030
}
3131

3232
/**
3333
* Configures the service.
3434
*/
3535
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')
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')
4141

42-
// Set the client ID and secret.
43-
.setClientId(CLIENT_ID)
44-
.setClientSecret(CLIENT_SECRET)
42+
// Set the client ID and secret.
43+
.setClientId(CLIENT_ID)
44+
.setClientSecret(CLIENT_SECRET)
4545

46-
// Set the name of the callback function that should be invoked to
47-
// complete the OAuth flow.
48-
.setCallbackFunction('authCallback')
46+
// Set the name of the callback function that should be invoked to
47+
// complete the OAuth flow.
48+
.setCallbackFunction('authCallback')
4949

50-
.setScope(scope)
50+
.setScope(scope)
5151

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-
;
52+
.setTokenHeaders({
53+
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
54+
})
55+
// Avoid "invalid_client error".
56+
// This service does not support form field authentication.
57+
.setTokenPayloadHandler(function(tokenPayload) {
58+
delete tokenPayload.client_id;
59+
return tokenPayload;
60+
})
61+
// Set the property store where authorized tokens should be persisted.
62+
.setPropertyStore(PropertiesService.getUserProperties())
63+
;
6264
}
6365

6466
/**
6567
* Handles the OAuth callback.
6668
*/
6769
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-
}
70+
var service = getService();
71+
var authorized = service.handleCallback(request);
72+
if (authorized) {
73+
return HtmlService.createHtmlOutput('Success!');
74+
} else {
75+
return HtmlService.createHtmlOutput('Denied.');
76+
}
7577
}
7678

7779
/**
7880
* Logs the redict URI to register.
7981
*/
8082
function logRedirectUri() {
81-
var service = getService();
82-
Logger.log(service.getRedirectUri());
83+
var service = getService();
84+
Logger.log(service.getRedirectUri());
8385
}

0 commit comments

Comments
 (0)