我正在尝试使用具有令牌授权的 API。
在他们的文档中说:
每个请求都应该包含一个带有 api 凭据的 Authorization 标头,如下所示:
Authorization: Token token="abc123", email="reseller@example.com"
我不确定我需要如何设置我的标题,我已经试过了
try {
const res = await fetch("https://pokamax.com/apis/reseller/v1/orders", {
method: "get",
headers: new Headers({
Authorization: 'Token token="mytoken", email="my@email.com',
// Authorization: "Bearer mytoken"
})
});
console.log("Res", res);
} catch (error) {
console.error("Error", error);
}
但这行不通。我需要如何设置我的标题?
答案 0 :(得分:0)
我不是 100% 确定,因为我无法阅读原始文档,但大多数标题会遵循类似于以下格式:
headers: new Headers({
Authorization: {
'Token': 'token="mytoken", email="my@email.com''
}
})
试一试,也许它会奏效。
答案 1 :(得分:0)
根据[1] Authorization header 必须具有以下结构:
Authorization: <type> <credentials>
Token
和 email
在这个标头上是不允许的类型,正如 MDN 所说的 [2]。我认为 API 的文档没有提供足够的信息。我认为电子邮件可能是一个单独的自定义标题。我不确定这会起作用,因为我不知道 API 是如何工作的,但请检查它是否有效:
try {
const res = await fetch("https://pokamax.com/apis/reseller/v1/orders", {
method: "get",
headers: new Headers({
Authorization: 'Bearer mytoken',
email: 'my@email.com'
})
});
console.log("Res", res);
} catch (error) {
console.error("Error", error);
}
[1] - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
[2] - https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes