-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
75 lines (66 loc) · 2.5 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
package main
import (
"context"
"fmt"
"os"
"github.com/coze-dev/coze-go"
)
// Using web oauth as example, show how to handle the Auth exception
func main() {
redirectURI := os.Getenv("COZE_WEB_OAUTH_REDIRECT_URI")
clientSecret := os.Getenv("COZE_WEB_OAUTH_CLIENT_SECRET")
clientID := os.Getenv("COZE_WEB_OAUTH_CLIENT_ID")
ctx := context.Background()
// 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
}
// 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.
oauth, err := coze.NewWebOAuthClient(clientID, clientSecret, coze.WithAuthBaseURL(cozeAPIBase))
if err != nil {
fmt.Printf("Failed to create OAuth client: %v\n", err)
return
}
// Generate the authorization link and direct the user to open it.
oauthURL := oauth.GetOAuthURL(ctx, &coze.GetWebOAuthURLReq{
RedirectURI: redirectURI,
State: "state",
})
fmt.Println(oauthURL)
// The space permissions for which the Access Token is granted can be specified. As following codes:
// oauthURL := oauth.GetOAuthURL(&coze.GetWebOAuthURLReq{
// RedirectURI: redirectURI,
// State: "state",
// WorkspaceID: &workspaceID,
// })
// fmt.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')
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.
resp, err := oauth.GetAccessToken(ctx, &coze.GetWebOAuthAccessTokenReq{
Code: code,
RedirectURI: redirectURI,
})
if err != nil {
fmt.Printf("Failed to get access token: %v\n", err)
// The SDK has enumerated existing authentication error codes
// You need to handle the exception and guide users to re-authenticate
// For different oauth type, the error code may be different,
// you should read document to get more information
authErr, ok := coze.AsAuthError(err)
if ok {
switch authErr.Code {
}
}
return
}
fmt.Println(resp)
}