javascript - 如何使用 API key 对 GitHub 进行身份验证?

标签 javascript authentication github github-api api-key

我正在尝试使用 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/

相关文章:

javascript - jQuery 日历无法正常工作

authentication - SSO(单点登录)如何工作

git - Maven 发布插件 git 凭据

c# - Visual Studio IIS 项目 HttpContext.Current.User.Identity.Current 为空

github - 如何将 png 等图像上传到 GitHub 存储库?

javascript - 我应该如何声明返回没有数据的 $q promise 的函数的返回类型?

javascript - 创建新数组和清除长度之间的区别

javascript - $ (":not(selector)")在普通/ Vanilla javascript中?

ios - 无法设置 cocoapods。 Cocoapods设置问题

Github:将额外的提交推送到 pull 请求