我正在尝试使用 GitHub Javascript API但无法使用我的 API key 进行身份验证。 API Docs说我可以使用:
{
username: 'my-username',
password: 'my-password'
// , token: 'or an optional oAuth token'
}
但我想改用我的 API key ,就像我可以从命令行使用 curl
一样。 (例如)
curl --user "$user:$api_key" --include --request DELETE "https://api.github.com/repos/$user/$repo/labels/enhancement"
我已尝试使用我的 API key 作为 token
,但这不起作用。
是否不支持使用 API key 通过 Github API 包装器访问 GitHub?那会很奇怪。
最佳答案
好吧,事实证明我调用的 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))
他的作品。
如果我输入一个垃圾 token new GitHub
不会提示(因为可能它实际上并没有像我最初想的那样去 GitHub 进行身份验证),但是 .listNotifications()
确实因 401
错误而被拒绝。
所以,这解决了我的困惑。
关于javascript - 如何使用 API key 对 GitHub 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41799040/