forked from coze-dev/coze-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-oauth-jwt.ts
82 lines (71 loc) · 2.46 KB
/
auth-oauth-jwt.ts
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
/* eslint-disable @typescript-eslint/naming-convention */
/*
* How to effectuate OpenAPI authorization through the OAuth JWT (JSON Web Token) 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 JWT 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 app ID, key ID, and audience can be obtained.
* Users also need to generate a private key and upload the corresponding public key to Coze.
*/
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import fs from 'fs';
import { CozeAPI, getJWTToken } from '@coze/api';
import { streamingChat } from '../utils.js';
import config from '../config/config.js';
import { botId } from '../client.js';
// 'en' for https://api.coze.com, 'cn' for https://api.coze.cn
const key = (process.env.COZE_ENV || 'en') as keyof typeof config;
// Retrieve configuration values from the config file
const baseURL = config[key].COZE_BASE_URL;
const appId = config[key].auth.oauth_jwt.COZE_APP_ID;
const keyid = config[key].auth.oauth_jwt.COZE_KEY_ID;
const aud = config[key].auth.oauth_jwt.COZE_AUD;
// Read the private key from a file
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const privateKey = fs
.readFileSync(join(__dirname, '../../tmp/private_key.pem'))
.toString();
let jwtToken = await getJWTToken({
baseURL,
appId,
aud,
keyid,
privateKey,
sessionName: 'test', // optional Isolate different sub-resources under the same jwt account
});
console.log('getJWTToken', jwtToken);
// Initialize a new Coze API client using the obtained access token
const client = new CozeAPI({
baseURL,
token: async () => {
// refresh token if expired
// 5 seconds buffer
if (jwtToken.expires_in * 1000 > Date.now() + 5000) {
return jwtToken.access_token;
}
console.log('refresh token');
jwtToken = await getJWTToken({
baseURL,
appId,
aud,
keyid,
privateKey,
sessionName: 'test',
});
return jwtToken.access_token;
},
});
// Example of how to use the client:
streamingChat({
client,
botId,
query: 'give me a joke',
});