我如何使用 GitHub REST API 通过我的个人访问令牌访问组织拥有的私有存储库中的文件数据?
How can I use the GitHub REST API to access a file's data in a private repository owned by an organization via my personal access token?
问题
我负责尝试从公司组织的私有存储库中的文件中检索 json 数据。我是该组织的所有者,但在使用从我的 GitHub 帐户生成的个人访问令牌时,总是收到 404
或 401
错误。
为什么我不能使用 OAuth
此节点应用程序将用作存储库中 GH 操作的一部分,并且用户将无法在每次推送到存储库时登录以验证访问权限。因此,为什么我正在寻找使用某种访问令牌的解决方案。
我尝试过的事情:
- 查看其他 Whosebug 问题,所有这些问题要么不相关、不起作用、没有回答,要么已过时。
- Accessing private repositories in organizations on Github Enterprise via REST(最接近我的问题,无解)
- 我尝试启用访问令牌的所有权限。 (其余的是在启用所有权限的情况下完成的)
- 在 raw.githubusercontent.com 和 api.github.com 之间切换,每次添加和删除
Accept
header 的 .raw
部分。
- 使用每个 URL 有和没有
Accept
header。
- 对每个 URL 使用
Authorization: "token TOKEN"
和 Authorization: "Bearer TOKEN"
。
- 使用我自己帐户中的存储库,这与我生成的访问令牌配合得很好。
我目前使用的代码
https
.get(
// "https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/path/data.json", // results in 404 error
"https://api.github.com/repos/ORG_NAME/REPO_NAME/contents/path/data.json", // results in 403 error
{
headers: {
// request the v3 version of the api
Accept: "application/vnd.github.v3.raw+json",
"Content-Type": "application/json;charset=UTF-8",
Authorization: `token ${process.env.GH_TOKEN}`,
},
},
res => {
const statusCode = res.statusCode;
const contentType = res.headers["content-type"] || "";
let error;
if (statusCode !== 200) {
error = new Error(
"Request Failed.\n" +
`Status Code: ${statusCode}: ${res.statusMessage}`
);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(
"Invalid content-type.\n" +
`Expected application/json but received ${contentType}`
);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", chunk => (rawData += chunk));
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
console.log(parsedData);
} catch (e) {
if (e instanceof Error) console.log(e.message);
}
});
})
.on("error", e => {
console.log(`Got error: ${e.message}`);
});
Sort-of“有效”但不是理想的解决方案
使用这个 URL: https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/path/data.json?token=SOME_RANDOM_GENERATED_TOKEN,在点击回购页面的 Raw 按钮后添加生成的令牌。我显然不想将此令牌用于生产操作。
我怎样才能让它工作?这个时候还有可能吗?
我尝试了以下请求,它有效。
GET 'https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/README.md' \
Authorization:'token <Personal Access Token>'
问题可能在请求 URL 中 - 看起来您在请求 URL 中包含了 /路径/,这不是必需的。您需要提供文件的实际路径,因此正确的 URL 将是
https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/data.json
问题
我负责尝试从公司组织的私有存储库中的文件中检索 json 数据。我是该组织的所有者,但在使用从我的 GitHub 帐户生成的个人访问令牌时,总是收到 404
或 401
错误。
为什么我不能使用 OAuth
此节点应用程序将用作存储库中 GH 操作的一部分,并且用户将无法在每次推送到存储库时登录以验证访问权限。因此,为什么我正在寻找使用某种访问令牌的解决方案。
我尝试过的事情:
- 查看其他 Whosebug 问题,所有这些问题要么不相关、不起作用、没有回答,要么已过时。
- Accessing private repositories in organizations on Github Enterprise via REST(最接近我的问题,无解)
- 我尝试启用访问令牌的所有权限。 (其余的是在启用所有权限的情况下完成的)
- 在 raw.githubusercontent.com 和 api.github.com 之间切换,每次添加和删除
Accept
header 的.raw
部分。 - 使用每个 URL 有和没有
Accept
header。 - 对每个 URL 使用
Authorization: "token TOKEN"
和Authorization: "Bearer TOKEN"
。 - 使用我自己帐户中的存储库,这与我生成的访问令牌配合得很好。
我目前使用的代码
https
.get(
// "https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/path/data.json", // results in 404 error
"https://api.github.com/repos/ORG_NAME/REPO_NAME/contents/path/data.json", // results in 403 error
{
headers: {
// request the v3 version of the api
Accept: "application/vnd.github.v3.raw+json",
"Content-Type": "application/json;charset=UTF-8",
Authorization: `token ${process.env.GH_TOKEN}`,
},
},
res => {
const statusCode = res.statusCode;
const contentType = res.headers["content-type"] || "";
let error;
if (statusCode !== 200) {
error = new Error(
"Request Failed.\n" +
`Status Code: ${statusCode}: ${res.statusMessage}`
);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(
"Invalid content-type.\n" +
`Expected application/json but received ${contentType}`
);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", chunk => (rawData += chunk));
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
console.log(parsedData);
} catch (e) {
if (e instanceof Error) console.log(e.message);
}
});
})
.on("error", e => {
console.log(`Got error: ${e.message}`);
});
Sort-of“有效”但不是理想的解决方案
使用这个 URL: https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/path/data.json?token=SOME_RANDOM_GENERATED_TOKEN,在点击回购页面的 Raw 按钮后添加生成的令牌。我显然不想将此令牌用于生产操作。
我怎样才能让它工作?这个时候还有可能吗?
我尝试了以下请求,它有效。
GET 'https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/README.md' \
Authorization:'token <Personal Access Token>'
问题可能在请求 URL 中 - 看起来您在请求 URL 中包含了 /路径/,这不是必需的。您需要提供文件的实际路径,因此正确的 URL 将是
https://raw.githubusercontent.com/ORG_NAME/REPO_NAME/main/data.json