-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
113 lines (97 loc) · 4.04 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"fmt"
"os"
"github.com/coze-dev/coze-go"
)
// This examples is about how to use the device 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 TVs/Limited Input devices/Command line programs.
// The specific creation process can be referred to in the document:
// https://www.coze.com/docs/developer_guides/oauth_device_code. For the cn environment, it can be
// accessed at https://www.coze.cn/docs/developer_guides/oauth_device_code.
// After the creation is completed, the client ID can be obtained.
func main() {
clientID := os.Getenv("COZE_DEVICE_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
cozeAPIBase := os.Getenv("COZE_API_BASE")
if cozeAPIBase == "" {
cozeAPIBase = coze.ComBaseURL
}
ctx := context.Background()
oauth, err := coze.NewDeviceOAuthClient(clientID, coze.WithAuthBaseURL(cozeAPIBase))
if err != nil {
fmt.Printf("Failed to create OAuth client: %v\n", err)
return
}
// In the device oauth authorization process, developers need to first call the interface
// of Coze to generate the device code to obtain the user_code and device_code. Then generate
// the authorization link through the user_code, guide the user to open the link, fill in the
// user_code, and consent to the authorization. Developers need to call the interface of Coze
// to generate the token through the device_code. When the user has not authorized or rejected
// the authorization, the interface will throw an error and return a specific error code.
// After the user consents to the authorization, the interface will succeed and return the
// access_token.
//
// First, make a call to obtain 'getDeviceCode'
codeResp, err := oauth.GetDeviceCode(ctx, nil)
if err != nil {
fmt.Printf("Failed to get device code: %v\n", err)
return
}
fmt.Printf("%+v\n", codeResp)
fmt.Println(codeResp.LogID())
// The space permissions for which the Access Token is granted can be specified. As following codes:
// codeResp, err = oauth.GetDeviceCode(ctx, &coze.GetDeviceOAuthCodeReq{
// WorkspaceID: &workspaceID,
// })
// The returned device_code contains an authorization link. Developers need to guide users
// to open up this link.
// open codeResp.getVerificationUri
fmt.Printf("Please open url: %s\n", codeResp.VerificationURL)
// The developers then need to use the device_code to poll Coze's interface to obtain the token.
// The SDK has encapsulated this part of the code in and handled the different returned error
// codes. The developers only need to invoke getAccessToken.
// if the developers set poll as true, the sdk will automatically handle pending and slow down exception
resp, err := oauth.GetAccessToken(ctx, &coze.GetDeviceOAuthAccessTokenReq{
DeviceCode: codeResp.DeviceCode,
Poll: true,
})
if err != nil {
authErr, ok := coze.AsAuthError(err)
if !ok {
fmt.Printf("Failed to get access token: %v\n", err)
return
}
switch authErr.Code {
case coze.AccessDenied:
// The user rejected the authorization.
// Developers need to guide the user to open the authorization link again.
fmt.Println("access denied")
case coze.ExpiredToken:
// The token has expired. Developers need to guide the user to open
// the authorization link again.
fmt.Println("expired token")
default:
fmt.Printf("Unexpected error: %v\n", err)
return
}
}
fmt.Printf("%+v\n", resp)
// // use the access token to init Coze client
// use the access token to init Coze client
cozeCli := coze.NewCozeAPI(coze.NewTokenAuth(resp.AccessToken))
_ = cozeCli
// When the token expires, you can also refresh and re-obtain the token
resp, err = oauth.RefreshToken(ctx, resp.RefreshToken)
if err != nil {
fmt.Printf("Failed to refresh token: %v\n", err)
return
}
fmt.Println(resp)
fmt.Println(resp.LogID())
}