【问题标题】:How do I authenticate against GitHub using an API key?如何使用 API 密钥对 GitHub 进行身份验证?
【发布时间】:2017-06-07 12:45:12
【问题描述】:

我正在尝试使用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 密钥通过 Github API 包装器访问 GitHub?那会很奇怪。

【问题讨论】:

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


    【解决方案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))
    

    它的工作原理。

    如果我扔垃圾令牌 new GitHub 不会抱怨(因为大概它实际上并没有像我最初想的那样去 GitHub 进行身份验证),但 .listNotifications() 确实被拒绝了401 错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2021-07-23
      相关资源
      最近更新 更多