最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problems Github Api Authorization - Stack Overflow

programmeradmin1浏览0评论

I'm trying to do a authorization request with Github Api, passing the username and password. But it's not working and i'm getting 401 status code.

In the Documentation there's a part saying

To use Basic Authentication with the GitHub API, simply send the username and password associated with the account.

That's my code:

this.api
        .post('/user', { username: 'Example', password: '1234' })
        .then(res => resolve(res.data))
        .catch(err => reject(err));

I'm trying to do a authorization request with Github Api, passing the username and password. But it's not working and i'm getting 401 status code.

In the Documentation there's a part saying

To use Basic Authentication with the GitHub API, simply send the username and password associated with the account.

That's my code:

this.api
        .post('/user', { username: 'Example', password: '1234' })
        .then(res => resolve(res.data))
        .catch(err => reject(err));
Share Improve this question edited Dec 15, 2019 at 20:23 Goran Stoyanov 2,3111 gold badge22 silver badges32 bronze badges asked Dec 13, 2019 at 13:35 Laura BeatrisLaura Beatris 1,9329 gold badges31 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4 +50

Not sure if you aim to use the Basic Authentication provided by Github API. If that's the case I think you should use the Axios auth header:

axios.get('https://example.', {
  auth: { user: "username", password: "password" }
});

Here's what Axios docs say:

  // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
  // This will set an `Authorization` header, overwriting any existing
  // `Authorization` custom headers you have set using `headers`.
  // Please note that only HTTP Basic auth is configurable through this parameter.
  // For Bearer tokens and such, use `Authorization` custom headers instead.
  auth: {
    username: 'janedoe',
    password: 's00pers3cret'
  },

There's another way to manually set the authorization header like this:

axios.get('https://example./', {
 headers: {
   Authorization: 'Basic ' + Base64.encode('username' + ':' + 'password');
 }
})

And the last note is that deprecation is ing:

Deprecation Notice: GitHub will discontinue password authentication to the API. You must now authenticate to the GitHub API with an API token, such as an OAuth access token, GitHub App installation access token, or personal access token, depending on what you need to do with the token.

Consider using tokens instead of username and password.

Note that if your account has activated 2FA (two-factor authentication), then you would need to use a PAT (Personal Access Token) as your password.

curl --header 'Authorization: token INSERTACCESSTOKENHERE' 
--header 'Accept: application/vnd.github.v3.raw' 
--remote-name 
--location https://api.github./...

See "Passing headers with axios POST request"

const headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/vnd.github.v3.raw',
  'Authorization': 'token INSERTACCESSTOKENHERE'
}

axios.post(url, data, {
    headers: headers
  })
  .then((response) => {
    dispatch({
      type: yourEvent,
      data: response.data[0]
    })
  })
  .catch((error) => {
    dispatch({
      type: yourError
    })
  })

Basic authentication requires you to add a header to the ajax request which gets send to the GitHub API. This is already answered in use-basic-authentication-with-jquery-and-ajax.

发布评论

评论列表(0)

  1. 暂无评论