如何使用API​​密钥对GitHub进行身份验证?

时间:2017-01-23 03:55:25

标签: javascript authentication github github-api api-key

我正在尝试使用GitHub Javascript API,但无法使用我的API密钥进行身份验证。 API Docs说我可以使用:

{
  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​​ Key通过不支持的Github API包装器访问GitHub?那会很奇怪。

1 个答案:

答案 0 :(得分:1)

好的,事实证明我所谓的API Key与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错误而被拒绝。

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

相关问题