Skip to content

Commit 3d9acc1

Browse files
yingtungchloechen
authored andcommitted
Support "Channel access token v2.1"
1 parent 117e897 commit 3d9acc1

File tree

3 files changed

+254
-3
lines changed

3 files changed

+254
-3
lines changed

linebot/api.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""linebot.api module."""
1616

17-
1817
import json
1918

2019
from .__about__ import __version__
@@ -31,7 +30,8 @@
3130
AudienceGroup, ClickAudienceGroup, ImpAudienceGroup, GetAuthorityLevel, Audience,
3231
CreateAudienceGroup
3332
)
34-
from .models.responses import Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse
33+
from .models.responses import Group, UserIds, RichMenuAliasResponse, RichMenuAliasListResponse, ChannelAccessTokens, \
34+
IssueChannelTokenResponseV2
3535

3636

3737
class LineBotApi(object):
@@ -1594,6 +1594,80 @@ def get_followers_ids(self, limit=300, start=None, timeout=None):
15941594

15951595
return UserIds.new_from_json_dict(response.json)
15961596

1597+
def issue_channel_access_token_v2_1(
1598+
self, client_assertion, grant_type='client_credentials',
1599+
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
1600+
timeout=None):
1601+
"""Issues a channel access token v2.1.
1602+
https://developers.line.biz/en/reference/messaging-api/#issue-channel-access-token-v2-1
1603+
:param str client_assertion: Client assertion.
1604+
:param str grant_type: `client_credentials`
1605+
:param str client_assertion_type: `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
1606+
:param timeout: (optional) How long to wait for the server
1607+
to send data before giving up, as a float,
1608+
or a (connect timeout, read timeout) float tuple.
1609+
Default is self.http_client.timeout
1610+
:type timeout: float | tuple(float, float)
1611+
:rtype: :py:class:`linebot.models.responses.IssueChannelTokenResponseV2`
1612+
"""
1613+
response = self._post(
1614+
'/oauth2/v2.1/token',
1615+
data={
1616+
'grant_type': grant_type,
1617+
'client_assertion_type': client_assertion_type,
1618+
'client_assertion': client_assertion,
1619+
},
1620+
headers={'Content-Type': 'application/x-www-form-urlencoded'},
1621+
timeout=timeout
1622+
)
1623+
1624+
return IssueChannelTokenResponseV2.new_from_json_dict(response.json)
1625+
1626+
def revoke_channel_access_token_v2_1(self, client_id,
1627+
client_secret, access_token,
1628+
timeout=None):
1629+
"""Revokes a channel access token v2.1.
1630+
https://developers.line.biz/en/reference/messaging-api/#revoke-channel-access-token-v2-1
1631+
:param str client_id: Client id.
1632+
:param str client_secret: Channel secret.
1633+
:param str access_token: Channel access token.
1634+
:param timeout: (optional) How long to wait for the server
1635+
to send data before giving up, as a float,
1636+
or a (connect timeout, read timeout) float tuple.
1637+
Default is self.http_client.timeout
1638+
:type timeout: float | tuple(float, float)
1639+
"""
1640+
self._post(
1641+
'/oauth2/v2.1/revoke',
1642+
data={'client_id': client_id,
1643+
'client_secret': client_secret,
1644+
'access_token': access_token},
1645+
timeout=timeout
1646+
)
1647+
1648+
def get_channel_access_tokens_v2_1(
1649+
self, client_assertion,
1650+
client_assertion_type='urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
1651+
timeout=None):
1652+
"""Get issued channel access tokens v2.1.
1653+
https://developers.line.biz/en/reference/messaging-api/#get-issued-channel-access-tokens-v2-1
1654+
:param str client_assertion: Client assertion.
1655+
:param str client_assertion_type: `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
1656+
:param timeout: (optional) How long to wait for the server
1657+
to send data before giving up, as a float,
1658+
or a (connect timeout, read timeout) float tuple.
1659+
Default is self.http_client.timeout
1660+
:type timeout: float | tuple(float, float)
1661+
:rtype: :py:class:`linebot.models.responses.ChannelAccessTokens`
1662+
"""
1663+
response = self._get(
1664+
'/oauth2/v2.1/tokens',
1665+
params={'client_assertion': client_assertion,
1666+
'client_assertion_type': client_assertion_type},
1667+
timeout=timeout
1668+
)
1669+
return ChannelAccessTokens.new_from_json_dict(response.json)
1670+
15971671
def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
15981672
url = (endpoint or self.endpoint) + path
15991673

@@ -1662,3 +1736,4 @@ def __check_error(response):
16621736
accepted_request_id=response.headers.get('X-Line-Accepted-Request-Id'),
16631737
error=Error.new_from_json_dict(response.json)
16641738
)
1739+

linebot/models/responses.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""linebot.models.responses module."""
1616

17-
1817
from .base import Base
1918
from .insight import (
2019
SubscriptionPeriodInsight, AppTypeInsight, AgeInsight,
@@ -843,3 +842,44 @@ def __init__(self, user_ids=None, next=None, **kwargs):
843842

844843
self.user_ids = user_ids
845844
self.next = next
845+
846+
847+
class IssueChannelTokenResponseV2(Base):
848+
"""IssueAccessTokenResponseV2.
849+
850+
https://developers.line.biz/en/reference/messaging-api/#issue-channel-access-token-v2-1
851+
"""
852+
853+
def __init__(self, access_token=None, expires_in=None, token_type=None, key_id=None, **kwargs):
854+
"""__init__ method.
855+
856+
:param str access_token: Short-lived channel access token.
857+
:param int expires_in: Time until channel access token expires in seconds
858+
from time the token is issued.
859+
:param str token_type: Bearer.
860+
:param key_id: Unique key ID for identifying the channel access token.
861+
:param kwargs:
862+
"""
863+
super(IssueChannelTokenResponseV2, self).__init__(**kwargs)
864+
865+
self.access_token = access_token
866+
self.expires_in = expires_in
867+
self.token_type = token_type
868+
self.key_id = key_id
869+
870+
871+
class ChannelAccessTokens(Base):
872+
"""ChannelAccessTokens.
873+
874+
https://developers.line.biz/ja/reference/messaging-api/#get-issued-channel-access-tokens-v2-1
875+
"""
876+
877+
def __init__(self, access_tokens=None, **kwargs):
878+
"""__init__ method.
879+
:param access_tokens: List of channel access token
880+
:type access_tokens: list[str]
881+
:param kwargs:
882+
"""
883+
super(ChannelAccessTokens, self).__init__(**kwargs)
884+
885+
self.access_tokens = access_tokens
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import sys
18+
import unittest
19+
20+
import responses
21+
22+
from linebot import (
23+
LineBotApi
24+
)
25+
26+
PY3 = sys.version_info[0] == 3
27+
if PY3:
28+
from urllib import parse
29+
else:
30+
import urlparse as parse
31+
32+
33+
class TestLineBotApi(unittest.TestCase):
34+
def setUp(self):
35+
self.tested = LineBotApi('channel_secret')
36+
self.access_token = 'W1TeHCgfH2Liwa.....'
37+
self.expires_in = 2592000
38+
self.token_type = 'Bearer'
39+
self.client_assertion = 'eyJhbGciOiJSUzI.q....'
40+
self.client_id = 'client_id'
41+
self.client_secret = 'client_secret'
42+
self.client_assertion_type = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'
43+
self.key_id = 'sDTOzw5wIfxxxxPEzcmeQA'
44+
45+
@responses.activate
46+
def test_issue_channel_access_token_v2_1(self):
47+
endpoint = LineBotApi.DEFAULT_API_ENDPOINT + '/oauth2/v2.1/token'
48+
responses.add(
49+
responses.POST,
50+
endpoint,
51+
json={
52+
'access_token': self.access_token,
53+
'expires_in': self.expires_in,
54+
'token_type': self.token_type,
55+
'key_id': self.key_id
56+
},
57+
status=200
58+
)
59+
60+
issue_access_token_response = self.tested.issue_channel_access_token_v2_1(
61+
self.client_assertion
62+
)
63+
64+
request = responses.calls[0].request
65+
self.assertEqual('POST', request.method)
66+
self.assertEqual(endpoint, request.url)
67+
self.assertEqual('application/x-www-form-urlencoded', request.headers['content-type'])
68+
self.assertEqual(self.access_token, issue_access_token_response.access_token)
69+
self.assertEqual(self.expires_in, issue_access_token_response.expires_in)
70+
self.assertEqual(self.token_type, issue_access_token_response.token_type)
71+
72+
encoded_body = parse.parse_qs(request.body)
73+
self.assertEqual('client_credentials', encoded_body['grant_type'][0])
74+
self.assertEqual(self.client_assertion_type, encoded_body['client_assertion_type'][0])
75+
self.assertEqual(self.client_assertion, encoded_body['client_assertion'][0])
76+
77+
@responses.activate
78+
def test_get_channel_access_token_v2_1(self):
79+
80+
endpoint = LineBotApi.DEFAULT_API_ENDPOINT + '/oauth2/v2.1/tokens'
81+
responses.add(
82+
responses.GET,
83+
endpoint,
84+
json={
85+
'access_tokens': [
86+
'fgIkeLcl3.....',
87+
'eyJhbGciO.....',
88+
'oeLklsSi7.....'
89+
]
90+
},
91+
status=200
92+
)
93+
channel_access_tokens_response = self.tested.get_channel_access_tokens_v2_1(
94+
self.client_assertion
95+
)
96+
97+
request = responses.calls[0].request
98+
self.assertEqual(request.method, 'GET')
99+
self.assertEqual(
100+
parse.unquote(request.url),
101+
parse.unquote('{}?client_assertion={}&client_assertion_type={}'.format(
102+
endpoint, self.client_assertion, self.client_assertion_type
103+
))
104+
)
105+
self.assertEqual(channel_access_tokens_response.access_tokens, [
106+
'fgIkeLcl3.....',
107+
'eyJhbGciO.....',
108+
'oeLklsSi7.....'
109+
])
110+
111+
@responses.activate
112+
def test_revoke_channel_access_token_v2_1(self):
113+
endpoint = LineBotApi.DEFAULT_API_ENDPOINT + '/oauth2/v2.1/revoke'
114+
115+
responses.add(
116+
responses.POST,
117+
endpoint,
118+
status=200
119+
)
120+
121+
self.tested.revoke_channel_access_token_v2_1(
122+
self.client_id, self.client_secret, self.access_token
123+
)
124+
125+
request = responses.calls[0].request
126+
self.assertEqual('POST', request.method)
127+
self.assertEqual(endpoint, request.url)
128+
129+
encoded_body = parse.parse_qs(request.body)
130+
self.assertEqual(self.client_id, encoded_body['client_id'][0])
131+
self.assertEqual(self.client_secret, encoded_body['client_secret'][0])
132+
self.assertEqual(self.access_token, encoded_body['access_token'][0])
133+
134+
135+
if __name__ == '__main__':
136+
unittest.main()

0 commit comments

Comments
 (0)