forked from citarreikee/connectMeTrae
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-github-oauth.js
More file actions
88 lines (74 loc) · 2.46 KB
/
test-github-oauth.js
File metadata and controls
88 lines (74 loc) · 2.46 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
const https = require('https');
const querystring = require('querystring');
require('dotenv').config();
// 测试GitHub OAuth配置
async function testGitHubOAuth() {
console.log('测试GitHub OAuth配置...');
const clientId = process.env.GITHUB_CLIENT_ID;
const clientSecret = process.env.GITHUB_CLIENT_SECRET;
const callbackUrl = process.env.GITHUB_CALLBACK_URL;
console.log('配置信息:');
console.log('Client ID:', clientId ? `${clientId.substring(0, 8)}...` : '未配置');
console.log('Client Secret:', clientSecret ? `${clientSecret.substring(0, 8)}...` : '未配置');
console.log('Callback URL:', callbackUrl);
if (!clientId || !clientSecret) {
console.error('GitHub OAuth配置不完整');
return;
}
// 测试GitHub API连接
try {
console.log('\n测试GitHub API连接...');
const response = await fetch('https://api.github.com/rate_limit');
const data = await response.json();
console.log('GitHub API连接正常,剩余请求次数:', data.rate.remaining);
} catch (error) {
console.error('GitHub API连接失败:', error.message);
}
// 模拟OAuth token交换请求
console.log('\n模拟OAuth token交换请求...');
const testCode = 'test_code_123';
const postData = querystring.stringify({
client_id: clientId,
client_secret: clientSecret,
code: testCode
});
const options = {
hostname: 'github.com',
port: 443,
path: '/login/oauth/access_token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
'Accept': 'application/json',
'User-Agent': 'CoderLink-Test'
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
console.log('响应状态码:', res.statusCode);
console.log('响应头:', res.headers);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('响应内容:', data);
try {
const jsonData = JSON.parse(data);
console.log('解析后的响应:', jsonData);
} catch (e) {
console.log('响应不是JSON格式');
}
resolve();
});
});
req.on('error', (error) => {
console.error('请求错误:', error);
resolve();
});
req.write(postData);
req.end();
});
}
testGitHubOAuth().catch(console.error);