-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathWebOAuthExample.java
More file actions
89 lines (75 loc) · 3.4 KB
/
Copy pathWebOAuthExample.java
File metadata and controls
89 lines (75 loc) · 3.4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package example.auth;
import com.coze.openapi.client.auth.OAuthToken;
import com.coze.openapi.service.auth.TokenAuth;
import com.coze.openapi.service.auth.WebOAuthClient;
import com.coze.openapi.service.config.Consts;
import com.coze.openapi.service.service.CozeAPI;
/*
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.
* */
public class WebOAuthExample {
public static void main(String[] args) {
String redirectURI = System.getenv("COZE_WEB_OAUTH_REDIRECT_URI");
String clientSecret = System.getenv("COZE_WEB_OAUTH_CLIENT_SECRET");
String clientID = System.getenv("COZE_WEB_OAUTH_CLIENT_ID");
/*
* 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
*/
String cozeAPIBase = System.getenv("COZE_API_BASE");
if (cozeAPIBase == null || cozeAPIBase.isEmpty()) {
cozeAPIBase = Consts.COZE_COM_BASE_URL;
}
/*
The sdk offers the WebOAuthClient class to establish an authorization for Web OAuth.
Firstly, it is required to initialize the WebOAuthApp with the client ID and client secret.
*/
WebOAuthClient oauth =
new WebOAuthClient.WebOAuthBuilder()
.clientID(clientID)
.clientSecret(clientSecret)
.baseURL(cozeAPIBase)
.build();
// Generate the authorization link and direct the user to open it.
String oauthURL = oauth.getOAuthURL(redirectURI, "state");
System.out.println(oauthURL);
/*
* The space permissions for which the Access Token is granted can be specified. As following codes:
* */
oauthURL = oauth.getOAuthURL(redirectURI, "state", "workspaceID");
System.out.println(oauthURL);
/*
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')
* */
String 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.
* */
OAuthToken resp = oauth.getAccessToken(code, redirectURI);
System.out.println(resp);
/*
* you can get request log by getLogID method
* */
System.out.println(resp.getLogID());
// use the access token to init Coze client
CozeAPI coze =
new CozeAPI.Builder()
.auth(new TokenAuth(resp.getAccessToken()))
.baseURL(cozeAPIBase)
.build();
// When the token expires, you can also refresh and re-obtain the token
resp = oauth.refreshToken(resp.getRefreshToken());
}
}