-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathauth_oauth_web.py
63 lines (47 loc) · 2.74 KB
/
auth_oauth_web.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
"""
How to effectuate OpenAPI authorization through the OAuth authorization code method.
"""
# 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 Web application.
# The specific creation process can be referred to in the document:
# https://www.coze.com/docs/developer_guides/oauth_code. For the cn environment, it can be
# accessed at https://www.coze.cn/docs/developer_guides/oauth_code.
# After the creation is completed, the client ID, client secret, and redirect link, can be
# obtained. For the client secret, users need to keep it securely to avoid leakage.
import os
from cozepy import COZE_COM_BASE_URL
# 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
web_oauth_client_id = os.getenv("COZE_WEB_OAUTH_CLIENT_ID")
# client secret
web_oauth_client_secret = os.getenv("COZE_WEB_OAUTH_CLIENT_SECRET")
# redirect link
web_oauth_redirect_uri = os.getenv("COZE_WEB_OAUTH_REDIRECT_URI")
# The sdk offers the WebOAuthApp class to establish an authorization for Web OAuth.
# Firstly, it is required to initialize the WebOAuthApp with the client ID and client secret.
from cozepy import Coze, TokenAuth, WebOAuthApp # noqa
web_oauth_app = WebOAuthApp(
client_id=web_oauth_client_id,
client_secret=web_oauth_client_secret,
base_url=coze_api_base,
)
# The WebOAuth authorization process is to first generate a coze authorization link and
# send it to the coze user requiring authorization. Once the coze user opens the link,
# they can see the authorization consent button.
# Generate the authorization link and direct the user to open it.
url = web_oauth_app.get_oauth_url(redirect_uri=web_oauth_redirect_uri)
# After the user clicks the authorization consent button, the coze web page will redirect
# to the redirect address configured in the authorization link and carry the authorization
# code and state parameters in the address via the query string.
# Get from the query of the redirect interface: query.get('code')
code = "mock code"
# After obtaining the code after redirection, the interface to exchange the code for a
# token can be invoked to generate the coze access_token of the authorized user.
oauth_token = web_oauth_app.get_access_token(redirect_uri=web_oauth_redirect_uri, code=code)
# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token), base_url=coze_api_base)
# When the token expires, you can also refresh and re-obtain the token
oauth_token = web_oauth_app.refresh_access_token(oauth_token.refresh_token)