2017-08-09 54 views
0

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

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

而且我有一个错误

This endpoint requires you to be authenticated. 

所以我试图用我的令牌authentificate,但它不工作。我试图例如做这样的:

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 

我也试图做到这一点在其他方面,但它不工作。

+0

您的'授权'标题只是'mytokenishere'还是它是'承载mytokenishere''? –

+0

嗯,这只是mytokenishere。 –

+2

尝试在令牌中包含'bearer'作为令牌类型。 GitHub v4 API文档在这里引用它:https://developer.github.com/v4/guides/forming-calls/#communicating-with-graphql,并且我假设'Bad credentials'表示它不是由于您不包含令牌类型,因此正确读取它。 –

回答

0

你必须包含这样的标记的类型。

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(); 
      } 
     }]); 
     }