|
|
|
|
|
|
|
class LineAPI { |
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function serializeParams_(params) { |
|
var paramsArray = []; |
|
for (var key in params) { |
|
paramsArray.push(key + '=' + encodeURIComponent(params[key])); |
|
} |
|
return paramsArray.join('&'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function promptUser_(authUrl) { |
|
var authCode = prompt('Please authorize and enter the auth code:', authUrl); |
|
return authCode; |
|
} |
|
} |