File size: 3,151 Bytes
c4a9a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * Line API class
 */
class LineAPI {
  /**
   * Get access token from Line
   * @return {string} access token
   */
  function getAccessToken() {
    var clientId = 'YOUR_CLIENT_ID';
    var clientSecret = 'YOUR_CLIENT_SECRET';
    var redirectUri = 'YOUR_REDIRECT_URI';
    var scope = 'profile openid email';
    var authUrl = 'https://access.line.me/oauth2/v2.1/authorize';
    var tokenUrl = 'https://api.line.me/oauth2/v2.1/token';
    
    var authCode = getAuthCode_(authUrl, clientId, redirectUri, scope);
    var tokenResponse = getToken_(tokenUrl, clientId, clientSecret, authCode);
    var accessToken = tokenResponse.access_token;
    return accessToken;
  }
  
  /**
   * Get user profile from Line
   * @param {string} accessToken
   * @return {object} user profile
   */
  function getUserProfile(accessToken) {
    var apiUrl = 'https://api.line.me/v2/profile';
    var headers = {
      'Authorization': 'Bearer ' + accessToken
    };
    var options = {
      'method': 'GET',
      'headers': headers
    };
    var response = UrlFetchApp.fetch(apiUrl, options);
    var userProfile = JSON.parse(response.getContentText());
    return userProfile;
  }
  
  /**
   * Get auth code from Line
   * @param {string} authUrl
   * @param {string} clientId
   * @param {string} redirectUri
   * @param {string} scope
   * @return {string} auth code
   */
  function getAuthCode_(authUrl, clientId, redirectUri, scope) {
    var authUrlParams = {
      'response_type': 'code',
      'client_id': clientId,
      'redirect_uri': redirectUri,
      'scope': scope
    };
    var authUrlWithParams = authUrl + '?' + encodeURI(serializeParams_(authUrlParams));
    var authCode = promptUser_(authUrlWithParams);
    return authCode;
  }
  
  /**
   * Get token from Line
   * @param {string} tokenUrl
   * @param {string} clientId
   * @param {string} clientSecret
   * @param {string} authCode
   * @return {object} token response
   */
  function getToken_(tokenUrl, clientId, clientSecret, authCode) {
    var tokenUrlParams = {
      'grant_type': 'authorization_code',
      'code': authCode,
      'redirect_uri': 'YOUR_REDIRECT_URI',
      'client_id': clientId,
      'client_secret': clientSecret
    };
    var options = {
      'method': 'POST',
      'headers': {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      'payload': serializeParams_(tokenUrlParams)
    };
    var response = UrlFetchApp.fetch(tokenUrl, options);
    var tokenResponse = JSON.parse(response.getContentText());
    return tokenResponse;
  }
  
  /**
   * Serialize parameters to URL query string
   * @param {object} params
   * @return {string} serialized parameters
   */
  function serializeParams_(params) {
    var paramsArray = [];
    for (var key in params) {
      paramsArray.push(key + '=' + encodeURIComponent(params[key]));
    }
    return paramsArray.join('&');
  }
  
  /**
   * Prompt user to authorize
   * @param {string} authUrl
   * @return {string} auth code
   */
  function promptUser_(authUrl) {
    var authCode = prompt('Please authorize and enter the auth code:', authUrl);
    return authCode;
  }
}