File size: 1,054 Bytes
5355a08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * OAuth2 service
 */
class OAuth2 {
  /**
   * Constructor
   * @param {string} clientId
   * @param {string} clientSecret
   */
  constructor(clientId, clientSecret) {
    this.clientId = clientId;
    this.clientSecret = clientSecret;
  }

  /**
   * Get access token
   * @return {string}
   */
  getAccessToken() {
    var service = OAuth2.createService('line-bot')
      .setAuthorizationBaseUrl('https://api.line.me/oauth2/v2.1')
      .setTokenUrl('https://api.line.me/oauth2/v2.1/token')
      .setClientId(this.clientId)
      .setClientSecret(this.clientSecret)
      .setCallbackFunction('authCallback')
      .setPropertyStore(PropertiesService.getUserProperties());
    return service.getAccessToken();
  }

  /**
   * Auth callback
   * @param {object} callback
   */
  authCallback(callback) {
    var authorized = callback.authorized;
    if (authorized) {
      return callback.accessToken;
    } else {
      var authorizationUrl = callback.authorizationUrl;
      Logger.log('Authorization URL: %s', authorizationUrl);
    }
  }
}