我正在创建一个 API 以使用 API 进行 GitHub 提交,使用 API 列出提交。 我的疑问是 我有一个存储库,如果该存储库是公共的,那么 API 可以完美运行。如果我将其更改为私有,则会给出“未找到”消息。访问的电子邮件不会在 Google 中“未找到”,那么当私有存储库找不到路由时,我该如何克服这个问题。我有可以访问的 emailid,如何使用 API 传递它?
const express = require('express');
const { Octokit } = require('@octokit/core');
const app = express();
const port = 3000;
app.get('/api/commits', async (req, res) => {
try {
const octokit = new Octokit({
auth: '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',
});
const owner = 'Praveenkumarsaravana';
const repo = 'django';
const response = await octokit.request('GET /repos/{owner}/{repo}/commits', {
owner: owner,
repo: repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
res.json(response.data);
} catch (error) {
console.error('Error:', error.message);
console.error('Error details:', error.response?.data);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/api/commit/:sha/branches', async (req, res) => {
try {
const octokit = new Octokit({
auth: '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',
});
const owner = 'Praveenkumarsaravana';
const repo = 'django';
const commit_sha = req.params.sha;
const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head', {
owner: owner,
repo: repo,
commit_sha: commit_sha,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
res.json(response.data);
} catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Get pull requests associated with a specific commit
app.get('/api/commit/:sha/pulls', async (req, res) => {
try {
console.log('Received request for /api/commit/:sha/pulls');
const octokit = new Octokit({
auth: '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',
});
const owner = 'Praveenkumarsaravana';
const repo = 'django';
const commit_sha = req.params.sha;
const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls', {
owner: owner,
repo: repo,
commit_sha: commit_sha,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
res.json(response.data);
} catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/commit/details/:sha', async (req, res) => {
try {
const octokit = new Octokit({
auth: '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',
});
const owner = 'Praveenkumarsaravana';
const repo = 'django';
// Check if req.body and req.body.ref are defined
// if (!req.body || !req.body.ref) {
// return res.status(400).json({ error: 'Missing or undefined "ref" in the request body' });
// }
const ref = req.params.ref;
const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{ref}', {
owner: owner,
repo: repo,
ref: ref,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
res.json(response.data);
} catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/compare/:basehead', async (req, res) => {
try {
const octokit = new Octokit({
auth: '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',
});
const owner = 'Praveenkumarsaravana';
const repo = 'django';
const basehead = req.params.basehead;
// if (!req.body || !req.body.basehead) {
// return res.status(400).json({ error: 'Missing or undefined "basehead" in the request body' });
// }
const response = await octokit.request('GET /repos/{owner}/{repo}/compare/{basehead}', {
owner: owner,
repo: repo,
basehead: basehead,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
res.json(response.data);
} catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
这是完整的index.js页面
http://localhost:3000/api/commits 上面是函数的路径,如果存储库是私有的,未找到,我如何传递凭据?
首先,从类似于
的代码中删除您的令牌const octokit = new Octokit({
auth: 'ghp_####################################',
});
现在.
其次,你的代码似乎没有错误,并且工作正常,试试这个:
令牌权限: 确保您使用的个人访问令牌具有必要的权限。转到您的 GitHub 帐户中的
Settings > Developer Settings > Personal access tokens
并确保令牌具有必要的权限。
代币范围: 如果修改token的权限,可能需要重新生成并使用新的token。
存储库访问: 确保与个人访问令牌关联的 GitHub 帐户有权访问私有存储库。如果您用于身份验证的电子邮件地址无权访问私有存储库,您将无法访问它。
检查拼写错误: 仔细检查代码中的存储库
name
、owner
和其他详细信息是否正确并与您尝试访问的私有存储库匹配。