2017-01-23 150 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错误而被拒绝。

所以,这解决了我的困惑。

相关问题