Javascript 如何使用API密钥对GitHub进行身份验证?
我正在尝试使用,但无法使用API密钥进行身份验证。我可以说:Javascript 如何使用API密钥对GitHub进行身份验证?,javascript,authentication,github,github-api,api-key,Javascript,Authentication,Github,Github Api,Api Key,我正在尝试使用,但无法使用API密钥进行身份验证。我可以说: { username: 'my-username', password: 'my-password' // , token: 'or an optional oAuth token' } 但是我想改用API键,就像我可以从命令行使用curl一样。(例如) 我已经尝试将我的API密钥用作令牌,但这不起作用 是否不支持使用API密钥通过GitHub API包装器访问GitHub?那会很奇怪。好吧,结果证明我调用的API密钥与
{
username: 'my-username',
password: 'my-password'
// , token: 'or an optional oAuth token'
}
但是我想改用API键,就像我可以从命令行使用curl
一样。(例如)
我已经尝试将我的API密钥用作令牌
,但这不起作用
是否不支持使用API密钥通过GitHub API包装器访问GitHub?那会很奇怪。好吧,结果证明我调用的API密钥与
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 }
我把\uu user:undefined
解释为身份验证不起作用
然而,如果我真的尝试,然后添加
me.listNotifications().then(
notifications => console.log('notifications', notifications),
err => console.log('error', err)
).catch(err => console.log('fatal', err))
塔达:这很有效
如果我扔了一个垃圾令牌new GitHub
,它不会抱怨(因为它可能不会像我第一次想的那样在那一点上去GitHub进行身份验证),但是.listNotifications()
会被拒绝,并出现401
错误
这就解决了我的困惑
me.listNotifications().then(
notifications => console.log('notifications', notifications),
err => console.log('error', err)
).catch(err => console.log('fatal', err))