Skip to content

Commit 05a87b2

Browse files
committed
Add an example of channel access token
1 parent 826c9f4 commit 05a87b2

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5+
6+
gem 'line-bot-api', path: '../../..' # delete this `path: ...`, and specify the latest version of line-bot-api when you want to use this example out side of this repository.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PATH
2+
remote: ../../..
3+
specs:
4+
line-bot-api (0.0.1.pre.test)
5+
base64 (~> 0.2)
6+
multipart-post (~> 2.4)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
base64 (0.2.0)
12+
multipart-post (2.4.1)
13+
14+
PLATFORMS
15+
arm64-darwin-21
16+
arm64-darwin-22
17+
18+
DEPENDENCIES
19+
line-bot-api!
20+
21+
BUNDLED WITH
22+
2.4.10
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Channel access token example
2+
3+
[Issue channel access token](https://developers.line.biz/en/docs/messaging-api/generate-json-web-token/)
4+
5+
## Getting started
6+
7+
```ruby
8+
$ export LINE_CHANNEL_ID=YOUR_CHANNEL_ID
9+
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
10+
$ bundle install
11+
$ bundle exec ruby app.rb
12+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)