-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathauth_oauth_jwt.py
65 lines (50 loc) · 2.66 KB
/
auth_oauth_jwt.py
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
"""
This example is about how to use the service jwt oauth process to acquire user authorization.
"""
# Firstly, users need to access https://www.coze.com/open/oauth/apps. For the cn environment,
# users need to access https://www.coze.cn/open/oauth/apps to create an OAuth App of the type
# of Service application.
# The specific creation process can be referred to in the document:
# https://www.coze.com/docs/developer_guides/oauth_jwt. For the cn environment, it can be
# accessed at https://www.coze.cn/docs/developer_guides/oauth_jwt.
# After the creation is completed, the client ID, private key, and public key id, can be obtained.
# For the client secret and public key id, users need to keep it securely to avoid leakage.
import os
from cozepy import COZE_COM_BASE_URL
from cozepy.auth import JWTAuth
# The default access is api.coze.com, but if you need to access api.coze.cn,
# please use base_url to configure the api endpoint to access
coze_api_base = os.getenv("COZE_API_BASE") or COZE_COM_BASE_URL
# client ID
jwt_oauth_client_id = os.getenv("COZE_JWT_OAUTH_CLIENT_ID")
# private key
jwt_oauth_private_key = os.getenv("COZE_JWT_OAUTH_PRIVATE_KEY")
# path to the private key file (usually with .pem extension)
jwt_oauth_private_key_file_path = os.getenv("COZE_JWT_OAUTH_PRIVATE_KEY_FILE_PATH")
# public key id
jwt_oauth_public_key_id = os.getenv("COZE_JWT_OAUTH_PUBLIC_KEY_ID")
if jwt_oauth_private_key_file_path:
with open(jwt_oauth_private_key_file_path, "r") as f:
jwt_oauth_private_key = f.read()
# The sdk offers the JWTOAuthApp class to establish an authorization for Service OAuth.
# Firstly, it is required to initialize the JWTOAuthApp.
from cozepy import Coze, TokenAuth, JWTOAuthApp # noqa
jwt_oauth_app = JWTOAuthApp(
client_id=jwt_oauth_client_id,
private_key=jwt_oauth_private_key,
public_key_id=jwt_oauth_public_key_id,
base_url=coze_api_base,
)
# The jwt oauth type requires using private to be able to issue a jwt token, and through
# the jwt token, apply for an access_token from the coze service. The sdk encapsulates
# this procedure, and only needs to use get_access_token to obtain the access_token under
# the jwt oauth process.
# Generate the authorization token
# The default ttl is 900s, and developers can customize the expiration time, which can be
# set up to 24 hours at most.
oauth_token = jwt_oauth_app.get_access_token(ttl=3600)
# use the jwt oauth_app to init Coze client
coze = Coze(auth=JWTAuth(oauth_app=jwt_oauth_app), base_url=coze_api_base)
# The jwt oauth process does not support refreshing tokens. When the token expires,
# just directly call get_access_token to generate a new token.
print(coze.workspaces.list().items)