繁体   English   中英

无法检索用户的范围:GitHub API 上的 email

[英]Cannot retrieve scopes for user:email on the GitHub API

我正在使用 node-fetch 从 OAuth2 GitHub 应用程序检索 OAuth2 令牌。 返回的令牌有效,我能够从“https://api.github.com/user”检索用户信息。 我还需要 email,这需要调用https://api.github.com/user/emails [0]

这需要一个 scope 的user:email ,我将其设置为参数:

app.get('/getAccessToken', async function (req, res) {
    const params = "?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + req.query.code + "&scope=user:email";

    await fetch("https://github.com/login/oauth/access_token" + params, {
        method: "POST",
        headers: {
            "Accept": "application/json"
        }
    }).then((response) => {
        return response.json();
    }).then(data => {
        res.json(data);
    });
});

问题是 scope 没有设置:

范围

我尝试在 header 中设置,但仍然没有快乐:

headers: {
            "Accept": "application/json",
            "X-OAuth-Scopes": "user:email"
        }

结果是 API 返回 404,表示授权权限:

{
  message: 'Not Found',
  documentation_url: 'https://docs.github.com/rest/reference/users#list-email-addresses-for-the-authenticated-user'
}

不过,我不明白这是怎么回事,因为我可以访问其他私人资源,例如私人仓库。

我认为这可能是权限问题,所以查看了 OAuth2 App 部分,我在其中配置了我的应用程序,但没有地方可以声明所需的范围

[0] https://docs.github.com/en/rest/users/emails?apiVersion=2022-11-28#list-email-addresses-for-the-authenticated-user

按照文档,使用Github SDK ,您可以找到一个向/user发送请求的示例,响应中的 object 包含 email 等信息。

 const octokit = new Octokit({ auth: 'YOUR-TOKEN' }) await octokit.request('GET /user', {})

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM