简体   繁体   English

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

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

I am using node-fetch retrieve an OAuth2 token from an OAuth2 GitHub App.我正在使用 node-fetch 从 OAuth2 GitHub 应用程序检索 OAuth2 令牌。 The token returned works, I am able to retrieve user information from "https://api.github.com/user".返回的令牌有效,我能够从“https://api.github.com/user”检索用户信息。 I also need the email, which requires a call to https://api.github.com/user/emails [0]我还需要 email,这需要调用https://api.github.com/user/emails [0]

This needs a scope of user:email which I set as a param:这需要一个 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);
    });
});

The problem is that the scope is not set:问题是 scope 没有设置:

范围

I tried setting in the header, but still no joy:我尝试在 header 中设置,但仍然没有快乐:

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

The result of this, is the API returns a 404 which is indicative of an auth permission:结果是 API 返回 404,表示授权权限:

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

I don't see how it can be this though, as I can access other private resources such as a private repo.不过,我不明白这是怎么回事,因为我可以访问其他私人资源,例如私人仓库。

I thought it might be a permissions thing, so had a look around the OAuth2 App section, where I configured my app, but there is no place to declare scopes required我认为这可能是权限问题,所以查看了 OAuth2 App 部分,我在其中配置了我的应用程序,但没有地方可以声明所需的范围

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

Following the documentation , using the Github SDK , you can find an example of get request to /user , the object in the response contains, among other information, the email.按照文档,使用Github SDK ,您可以找到一个向/user发送请求的示例,响应中的 object 包含 email 等信息。

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

I hope this helps!我希望这有帮助!

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

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