对 GitHub 的 GraphQL 查询因 HTTP 422 无法处理的实体而失败
GraphQL query to GitHub failing with HTTP 422 Unprocessable Entity
我目前正在使用 NodeJS 开发一个简单的 GitHub GraphQL 客户端。
鉴于 GitHub GraphQL API 只能通过访问令牌访问,我设置了一个 OAuth2 请求来获取访问令牌,然后尝试触发一个简单的 GraphQL 查询。
OAuth2 流程为我提供了令牌,但是当我发送查询时,我收到 HTTP 422。
下面是我自己的代码的简化片段:
- 准备 URL 显示在 UI 侧,让用户单击它并使用 GitHub
执行登录
getGitHubAuthenticationURL(): string {
const searchParams = new URLSearchParams({
client_id,
state,
login,
scope,
});
return `https://github.com/login/oauth/authorize?${searchParams}`;
}
- 我的 ExpressJs 服务器监听 GitHub OAuth2 响应
httpServer.get("/from-github/oauth-callback", async (req, res) => {
const {
query: { code, state },
} = req;
const accessToken = await requestGitHubAccessToken(code as string);
[...]
});
- 请求访问令牌
async requestToken(code: string): Promise<string> {
const { data } = await axios.post(
"https://github.com/login/oauth/access_token",
{
client_id,
client_secret,
code
},
{
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
}
);
return data.access_token;
}
- 触发简单的 graphql 查询
const data = await axios.post(
"https://graphql.github.com/graphql/proxy",
{ query: "{ viewer { login } }"},
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}
);
你们有什么线索吗?
也许我在 OAuth2 流程上做错了什么?与我在网上找到的大多数示例一样,个人令牌用于此目的,在 GitHub 上生成,但我想改用 OAuth2。
在此先感谢您的帮助,非常感谢!
编辑
我将查询从 { query: "query { viewer { login } }"}
更改为 { query: "{ viewer { login } }"}
,但问题仍然存在。
我终于找到了解决方案:
- 将 URL 从
https://graphql.github.com/graphql/proxy
更改为 https://api.github.com/graphql
,请参阅 here
- 添加以下 HTTP headers
- "Content-Type": "application/json"
- "Content-Length"
- "User-Agent"
希望这对其他人有帮助。
我目前正在使用 NodeJS 开发一个简单的 GitHub GraphQL 客户端。
鉴于 GitHub GraphQL API 只能通过访问令牌访问,我设置了一个 OAuth2 请求来获取访问令牌,然后尝试触发一个简单的 GraphQL 查询。
OAuth2 流程为我提供了令牌,但是当我发送查询时,我收到 HTTP 422。
下面是我自己的代码的简化片段:
- 准备 URL 显示在 UI 侧,让用户单击它并使用 GitHub 执行登录
getGitHubAuthenticationURL(): string {
const searchParams = new URLSearchParams({
client_id,
state,
login,
scope,
});
return `https://github.com/login/oauth/authorize?${searchParams}`;
}
- 我的 ExpressJs 服务器监听 GitHub OAuth2 响应
httpServer.get("/from-github/oauth-callback", async (req, res) => {
const {
query: { code, state },
} = req;
const accessToken = await requestGitHubAccessToken(code as string);
[...]
});
- 请求访问令牌
async requestToken(code: string): Promise<string> {
const { data } = await axios.post(
"https://github.com/login/oauth/access_token",
{
client_id,
client_secret,
code
},
{
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
}
);
return data.access_token;
}
- 触发简单的 graphql 查询
const data = await axios.post(
"https://graphql.github.com/graphql/proxy",
{ query: "{ viewer { login } }"},
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}
);
你们有什么线索吗? 也许我在 OAuth2 流程上做错了什么?与我在网上找到的大多数示例一样,个人令牌用于此目的,在 GitHub 上生成,但我想改用 OAuth2。
在此先感谢您的帮助,非常感谢!
编辑
我将查询从 { query: "query { viewer { login } }"}
更改为 { query: "{ viewer { login } }"}
,但问题仍然存在。
我终于找到了解决方案:
- 将 URL 从
https://graphql.github.com/graphql/proxy
更改为https://api.github.com/graphql
,请参阅 here - 添加以下 HTTP headers
- "Content-Type": "application/json"
- "Content-Length"
- "User-Agent"
希望这对其他人有帮助。