2017-01-23 86 views
2

我正在嘗試使用GitHub Javascript API,但無法使用我的API密鑰進行身份驗證。該API Docs說,我可以使用:如何使用API​​密鑰對GitHub進行身份驗證?

{ 
    username: 'my-username', 
    password: 'my-password' 
    // , token: 'or an optional oAuth token' 
} 

,但我想用我的API密鑰代替,就像我可以curl在命令行中執行。 (例如)

curl --user "$user:$api_key" --include --request DELETE "https://api.github.com/repos/$user/$repo/labels/enhancement" 

我已經用我的API密鑰爲token試過,但不起作用。

是否使用API​​密鑰通過不支持的Github API包裝來訪問GitHub?那會很奇怪。

回答

1

好了,原來我一直在打電話給我的API密鑰是同樣的事情personal access token,我只是被迷惑,因爲

import GitHub from 'github-api' 

const gh = new GitHub({ 
    token: 'MY-PERSONAL-ACCESS-TOKEN-OBTAINED-FROM-github.com/settings/tokens' // real token redacted obviously 
}) 

const me = gh.getUser() 
console.log('me', me) 

被吐出

me User { 
    __apiBase: 'https://api.github.com', 
    __auth: 
    { token: '970818535b841883ff2735fe127d289032970389', 
    username: undefined, 
    password: undefined }, 
    __AcceptHeader: 'v3', 
    __authorizationHeader: 'token MY-PERSONAL-ACCESS-TOKEN-OBTAINED-FROM-github.com/settings/tokens', 
    __user: undefined } 

,我是將__user: undefined解釋爲意味着身份驗證不起作用。

但是如果我真的嘗試再加入

me.listNotifications().then(
    notifications => console.log('notifications', notifications), 
    err => console.log('error', err) 
).catch(err => console.log('fatal', err)) 

tadah它的工作原理。

如果我扔在一個垃圾令牌new GitHub不抱怨(因爲那時它實際上沒有去上GitHub上做到該點的權威性,因爲我首先想到的),但.listNotifications()確實得到了401錯誤而被拒絕。

所以,這解決了我的困惑。

相關問題