2

我正在尝试使用apps-script-oauth2库来访问 Google Apps 脚本中的 GitHub API。我正在创建这样的服务:

function getGithubService(client_id, client_secret) {
  return OAuth2.createService("GitHub")
    .setAuthorizationBaseUrl("https://github.com/login/oauth/authorize")
    .setTokenUrl("https://github.com/login/oauth/access_token")
    .setClientId(client_id)
    .setClientSecret(client_secret)
    .setCallbackFunction("authCallback")
    .setPropertyStore(PropertiesService.getUserProperties())
    .setCache(CacheService.getUserCache())
    .setLock(LockService.getUserLock())
    .setScope("repo");
}

function authCallback(request) {
  let github_service = getGithubService();
  if (github_service.handleCallback(request)) {
    return HtmlService.createHtmlOutput("Success!");
  }
  return false;
}

我可以启动身份验证端点并获取有效的访问令牌。但是我得到的令牌似乎没有被授权用于任何范围。我可以记录令牌并尝试在 curl 请求中使用它:

$ token=ghu_<censored>
$ curl -i -H "Authorization: bearer ${token}" https://api.github.com/user
...
x-oauth-scopes: 
x-accepted-oauth-scopes: 
...

这样做的结果是,我对需要授权范围的资源的任何请求要么失败,要么返回一个空列表。特别是,即使我在上面的服务配置中指定了范围,/org/{org-name}/repos端点也会返回。[]repo

(更新:我的组织只有私有存储库;如果我为具有公共存储库的组织请求此端点,我会得到一个可用的存储库列表。显然我需要repo获取私有存储库的范围。)

我要求这样的回购列表:

  let service = getGithubService(client_id, client_secret);
  let org_name = "...";
  let url = `https://api.github.com/orgs/${org_name}/repos`;
  let response = UrlFetchApp.fetch(url, {
    headers: {
      Authorization: `token ${service.getAccessToken()}`,
      Accept: "application/vnd.github.v3+json"
    }
  });
  let repos = JSON.parse(response.getContentText());

在可能相关的一点上,是否应该有某种方法将 OAuth2 请求中使用的范围名称与您可以在 GitHub 开发人员设置页面中授予应用程序的权限相关联?我完全不确定我是否已授予 GitHub 应用程序正确的权限集,以便能够repo在身份验证请求中请求范围,但似乎根本没有任何文档说明两者之间的关系。

4

0 回答 0