问题 Github Api 授权
Problems Github Api Authorization
我正在尝试使用 Github Api 进行授权请求,并传递用户名和密码。
但它不起作用,我收到 401 状态代码。
在 Documentation 中有一部分说
To use Basic Authentication with the GitHub API, simply send the username and password associated with the account.
这是我的代码:
this.api
.post('/user', { username: 'Example', password: '1234' })
.then(res => resolve(res.data))
.catch(err => reject(err));
不确定您是否打算使用 Github API 提供的基本身份验证。如果是这种情况,我认为您应该使用 Axios auth
header:
axios.get('https://example.com', {
auth: { user: "username", password: "password" }
});
Axios 文档是这样说的:
// `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'
},
还有另一种手动设置权限的方法header,如下所示:
axios.get('https://example.com/', {
headers: {
Authorization: 'Basic ' + Base64.encode('username' + ':' + 'password');
}
})
最后一点是即将弃用:
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.
考虑使用令牌代替用户名和密码。
请注意,如果您的帐户已激活 2FA (two-factor authentication), then you would need to use a PAT (Personal Access Token) 作为您的密码。
curl --header 'Authorization: token INSERTACCESSTOKENHERE'
--header 'Accept: application/vnd.github.v3.raw'
--remote-name
--location https://api.github.com/...
参见“”
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
})
})
基本身份验证要求您将 header 添加到 ajax 请求,然后发送到 GitHub API。 use-basic-authentication-with-jquery-and-ajax.
中已经回答了这个问题
我正在尝试使用 Github Api 进行授权请求,并传递用户名和密码。 但它不起作用,我收到 401 状态代码。
在 Documentation 中有一部分说
To use Basic Authentication with the GitHub API, simply send the username and password associated with the account.
这是我的代码:
this.api
.post('/user', { username: 'Example', password: '1234' })
.then(res => resolve(res.data))
.catch(err => reject(err));
不确定您是否打算使用 Github API 提供的基本身份验证。如果是这种情况,我认为您应该使用 Axios auth
header:
axios.get('https://example.com', {
auth: { user: "username", password: "password" }
});
Axios 文档是这样说的:
// `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' },
还有另一种手动设置权限的方法header,如下所示:
axios.get('https://example.com/', {
headers: {
Authorization: 'Basic ' + Base64.encode('username' + ':' + 'password');
}
})
最后一点是即将弃用:
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.
考虑使用令牌代替用户名和密码。
请注意,如果您的帐户已激活 2FA (two-factor authentication), then you would need to use a PAT (Personal Access Token) 作为您的密码。
curl --header 'Authorization: token INSERTACCESSTOKENHERE'
--header 'Accept: application/vnd.github.v3.raw'
--remote-name
--location https://api.github.com/...
参见“
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
})
})
基本身份验证要求您将 header 添加到 ajax 请求,然后发送到 GitHub API。 use-basic-authentication-with-jquery-and-ajax.
中已经回答了这个问题