【问题标题】:Axios is not working to get access token?Axios 无法获取访问令牌?
【发布时间】:2022-01-18 23:57:53
【问题描述】:

我有 axios 调用来获取承载令牌,它是 oauth2.0,但由于某种原因它失败了,同样的调用正在邮递员中工作。

index.js

export async function getESLAccessToken(apiConfig: any) {
    const _body = {
        grant_type: apiConfig.authOptions.body.grant_type,
        client_id: apiConfig.authOptions.credentials.sdk_voyage.clientId,
        client_secret: apiConfig.authOptions.credentials.sdk_voyage.clientSecret,
        scope: apiConfig.authOptions.body.scope
    };
    const _headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "appName": "Blink",
        "Accept": "*/*",
        "Content-Length": 163
    };
    const request = {
        method: 'POST',
         _body,
        headers: _headers
    };

    try {
        const resp = await axios.post('https://auth/oauth2/token',
        request);
        console.log(resp.data);
    } catch (err) {
        // Handle Error Here
        throw err;
    }
}

这就是为 axios 构建请求的方式

{
  "method": "POST",
  "_body": {
    "grant_type": "client_credentials",
    "client_id": "abc",
    "client_secret": "xyz",
    "scope": "APPPII APPPHI"
  },
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "appName": "Blink",
    "Accept": "*/*",
    "Content-Length": 163
  }
}

错误

Error: Request failed with status code 400

邮递员工作请求和响应

POST https://auth/oauth2/token
200
163 ms
POST /auth/oauth2/token HTTP/1.1

Headers
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Postman-Token: d90ccc33-1d77-4502-9a41-74080dd3d7a5
Host: qaapih8.corp.cvscaremark.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 163
Request Body
grant_type=client_credentials&client_id=abc&client_secret=xyz&scope=APPPII%20APPPHI
HTTP/1.1 200 OK

响应正文

{ "token_type":"Bearer", "access_token":"token returned", "expires_in":3600, "consented_on":164684, "scope":"APPPII APPPHI" }

【问题讨论】:

标签: javascript node.js oauth-2.0 axios


【解决方案1】:

axios.post 函数的第二个参数应该是请求正文。第三个参数应该是 Axios 请求配置对象(您可以设置标头)。 Axios POST Requests

在你的情况下,代码应该是这样的:

const body = {
  grant_type: apiConfig.authOptions.body.grant_type,
  client_id: apiConfig.authOptions.credentials.sdk_voyage.clientId,
  client_secret: apiConfig.authOptions.credentials.sdk_voyage.clientSecret,
  scope: apiConfig.authOptions.body.scope
};

const myHeaders = {
  // add your headers here
}

const response = await axios.post(
  'https://auth/oauth2/token',
  body,
  { headers: myHeaders }
);

【讨论】:

    【解决方案2】:

    替换

    const request = {
        method: 'POST',
         _body,
        headers: _headers
    };
    

    与:

    const request = {
        method: 'POST',
        data: _body,
        headers: _headers
    };
    

    正如 cmets 会话中的文档所指出的:https://axios-http.com/docs/req_config

    【讨论】:

    • 我试过这个它没有同样的错误
    猜你喜欢
    • 2020-11-06
    • 2017-06-02
    • 2022-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    相关资源
    最近更新 更多