2

I am trying to use the GitHub Javascript API but am unable to authenticate using my API key. The API Docs say I can use:

{
  username: 'my-username',
  password: 'my-password'
  // , token: 'or an optional oAuth token'
}

But I'd like to use my API Key instead, much like I can do with curl from the command line. (for example)

curl --user "$user:$api_key" --include --request DELETE "https://api.github.com/repos/$user/$repo/labels/enhancement"

I've tried using my API Key as the token but that doesn't work.

Is use of the API Key to access GitHub via the Github API wrapper not supported? That would be weird.

1 Answer 1

1

Okay so it turns out what I was calling my API Key is the same thing as the personal access token and I was just being confused because

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)

was spitting out

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 }

and I was interpreting __user: undefined to mean that the authentication was not working.

However if I actually try then adding

me.listNotifications().then(
    notifications => console.log('notifications', notifications),
    err => console.log('error', err)
).catch(err => console.log('fatal', err))

tadah it works.

If I throw in a rubbish token new GitHub doesn't complain (because presumably it's not actually going off to GitHub to do the auth at that point as I first thought), but the .listNotifications() does get rejected with a 401 error.

So, that's resolved my confusion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.