2017-08-09 70 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(); 
      } 
     }]); 
     }