|
| 1 | +require 'line-bot-api' |
| 2 | + |
| 3 | +def client |
| 4 | + # NOTE: Don't need to set `channel_access_token` and `channel_secret` in the client. |
| 5 | + @client ||= Line::Bot::V2::ChannelAccessToken::ApiClient.new |
| 6 | +end |
| 7 | + |
| 8 | +def main |
| 9 | + access_token = issue_channel_access_token |
| 10 | + |
| 11 | + if access_token |
| 12 | + sleep 1 |
| 13 | + verify_channel_access_token(access_token) |
| 14 | + revoke_channel_access_token(access_token) |
| 15 | + verify_channel_access_token(access_token) |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +def issue_channel_access_token |
| 20 | + response = client.issue_channel_token( |
| 21 | + grant_type: 'client_credentials', # fixed value |
| 22 | + client_id: ENV.fetch('LINE_CHANNEL_ID'), |
| 23 | + client_secret: ENV.fetch('LINE_CHANNEL_SECRET') |
| 24 | + ) |
| 25 | + |
| 26 | + if response.is_a?(Line::Bot::V2::ChannelAccessToken::IssueShortLivedChannelAccessTokenResponse) |
| 27 | + puts "=== access_token has been issued successfully ===" |
| 28 | + puts "access_token: #{response.access_token}" |
| 29 | + puts "expires_in: #{response.expires_in}" |
| 30 | + puts "token_type: #{response.token_type}" |
| 31 | + |
| 32 | + response.access_token |
| 33 | + else |
| 34 | + puts "=== access_token has not been issued ===" |
| 35 | + nil |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +def verify_channel_access_token(accesss_token) |
| 40 | + response = client.verify_channel_token(access_token: accesss_token) |
| 41 | + |
| 42 | + if response.is_a?(Line::Bot::V2::ChannelAccessToken::VerifyChannelAccessTokenResponse) |
| 43 | + puts "=== access_token has been verified successfully ===" |
| 44 | + puts "client_id: #{response.client_id}" |
| 45 | + puts "expires_in: #{response.expires_in}" |
| 46 | + puts "scope: #{response.scope}" |
| 47 | + else |
| 48 | + puts "=== access_token has not been verified ===" |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +def revoke_channel_access_token(accesss_token) |
| 53 | + _body, status_code, _http_headers = client.revoke_channel_token_with_http_info(access_token: accesss_token) |
| 54 | + if status_code == 200 |
| 55 | + puts "=== access_token has been revoked successfully ===" |
| 56 | + else |
| 57 | + puts "=== access_token revoking failed ===" |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +main |
0 commit comments