【问题标题】:GitHub API v4 Graphql: Get current authorized user organizations and their repositoriesGitHub API v4 Graphql:获取当前授权用户组织及其存储库
【发布时间】:2020-09-26 02:38:33
【问题描述】:

在具有 repouser 授权范围的 GitHub API v3 中,我可以使用 GET /user/orgshttps://developer.github.com/v3/orgs/#list-organizations-for-the-authenticated-user,使用 Octokit REST JS,octokit.orgs.listForAuthenticatedUser() ) 并为每个组织获取我可以访问的存储库,GET /orgs/:org/reposhttps://developer.github.com/v3/repos/#list-organization-repositories,使用 Octokit,octokit.repos.listForOrg({ org: orgs.data[i].login }))。

但是,使用相同的身份验证范围(用户和存储库),运行此 Graphql 查询

query getOrgsRepos {
  viewer {
    organizations(first: 10) {
      nodes {
        repositories(first: 10) {
          nodes {
            name
          }
        }
      }
    }
  }
}

返回

{
  "data": {
    "viewer": {
      "organizations": {
        "nodes": []
      }
    }
  }
}

Graphql Explorer 结果 (https://developer.github.com/v4/explorer/),但在我的 JS authed(userrepo 范围)应用程序上运行返回相同的空结果

如何在 API v4 中实现相同的行为,无需授予更多权限

【问题讨论】:

    标签: github graphql github-api octokit-js


    【解决方案1】:

    我今天刚遇到这个问题。不幸的是,截至本答案底部显示的时间戳,GitHub 的 GraphQL API 与其 REST API 不相上下。以下查询只会产生查看者的公共组织,即未经身份验证的会话将在其 github.com 个人资料的“组织”下列出的内容。

    query getOrgs {
      viewer {
        organizations(first: 100) {
          totalCount
          nodes {
            name
          }
        }
      }
    }
    

    并且在他们的 GraphQL 架构中没有等效的 octokit.orgs.listForAuthenticatedUser(),它基本上获取 REST 端点 /user/orgs,以列出经过身份验证的用户的组织。来自the docs

    /user/orgs 仅列出您的授权允许您的组织 以某种方式进行操作(例如,您可以使用 read:org 列出团队 范围,您可以使用用户范围公开您的组织成员资格, 等等。)。因此,此 API 至少需要 user 或 read:org 范围。 范围不足的 OAuth 请求收到 403 Forbidden 回应。

    换句话说,使用具有足够范围的个人访问令牌,/user/orgs 返回与Your Organizations 页面上显示的相同列表。如果您使用 OAuth 访问令牌进行身份验证,则该列表与您的 OAuth 应用的用户 Authorized OAuth Apps 页面上的“组织访问”下显示的几乎相同。

    【讨论】:

      【解决方案2】:

      我认为您的查询有问题,缺少组织login

      query getOrgsRepos {
        viewer {
          organizations(first: 10) {
            nodes {
              login
              repositories(first: 10) {
                nodes {
                  name
                }
              }
            }
          }
        }
      }
      

      我只收到那些范围的响应。

      【讨论】:

        猜你喜欢
        • 2020-10-18
        • 2018-03-26
        • 2023-04-07
        • 2018-12-05
        • 2016-02-12
        • 2021-08-29
        • 2019-12-11
        • 2017-11-15
        • 1970-01-01
        相关资源
        最近更新 更多