【问题标题】:GitHub API v4 authentificationGitHub API v4 认证
【发布时间】:2018-01-17 09:36:22
【问题描述】:

我从 GitHub 生成了一个令牌,我想使用 GitHub API v4,但我必须先进行身份验证。我试过这段代码:

const networkInterface = createBatchingNetworkInterface({
  uri: 'https://api.github.com/graphql',
  batchInterval: 10
});

我有一个错误

This endpoint requires you to be authenticated.

所以我正在尝试使用我的令牌进行身份验证,但它不起作用。我在示例中尝试这样做:

networkInterface.use([{
  applyMiddleware(req, next) {
    if (!req.options.headers) {
      req.options.headers = {};  // Create the header object if needed.
    }
    req.options.headers['Authorization'] = 'mytokenishere';
    next();
  }
}]);

在这种情况下,我会收到一条信息:

Bad credentials

我也尝试过用其他方式来做到这一点,但它不起作用。

【问题讨论】:

  • 您的Authorization 标头只是mytokenishere 还是bearer mytokenishere
  • 嗯,这只是 mytokenishere。
  • 尝试在您的令牌中包含bearer 作为令牌类型。 GitHub v4 API 文档在这里引用了它:developer.github.com/v4/guides/forming-calls/…,我假设Bad credentials 表示它没有正确读取它,因为你没有包含令牌类型。

标签: angular authentication github-api apollo


【解决方案1】:

你必须像这样包含令牌的类型。

  setNetworkInterface(): void{
        networkInterface.use([{
          applyMiddleware(req, next) {
            if (!req.options.headers) {
              req.options.headers = {}; 
            }
            req.options.headers.authorization = 'Bearer XXXXXXXXXXX';
            req.options.headers.host = 'https://api.github.com/graphql';
            next();
          }
        }]);
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-10
    • 2019-04-13
    • 2018-06-08
    • 2020-01-12
    • 1970-01-01
    • 2018-08-21
    • 2023-03-08
    • 2018-10-01
    相关资源
    最近更新 更多