@@ -2,82 +2,84 @@ var CLIENT_ID = '...';
22var 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 */
77function 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 */
2727function reset ( ) {
28- var service = getService ( ) ;
29- service . reset ( ) ;
28+ var service = getService ( ) ;
29+ service . reset ( ) ;
3030}
3131
3232/**
3333 * Configures the service.
3434 */
3535function 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 */
6769function 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 */
8082function logRedirectUri ( ) {
81- var service = getService ( ) ;
82- Logger . log ( service . getRedirectUri ( ) ) ;
83+ var service = getService ( ) ;
84+ Logger . log ( service . getRedirectUri ( ) ) ;
8385}
0 commit comments