简体   繁体   English

内容类型 header 缺失或不受支持

[英]content-type header missing or unsupported

I get this error, when I want to get a new refresh token from autodesk.当我想从 Autodesk 获取新的刷新令牌时出现此错误。 https://forge.autodesk.com/en/docs/oauth/v2/reference/http/refreshtoken-POST/ https://forge.autodesk.com/en/docs/oauth/v2/reference/http/refreshtoken-POST/

 developerMessage: 'content-type header missing or unsupported content-type used, must use application/x-www-form-urlencoded'
bim360Router.use(async (req, res, next) => {
    //verify if forge token is set in the cookies and not expired
    let accessToken = req.cookies['access_token'];
    let refresh_token = req.cookies['refresh_token'];
    if(refresh_token && !accessToken) { //if token is expired,  then generate a new token if refresh token is set 
         const config ={
            headers : {
                'content-type': 'application/x-www-form-urlencoded',
            },        
            params : {
                client_id: process.env.FORGE_CLIENT_ID,
                client_secret: process.env.FORGE_CLIENT_SECRET,
                refresh_token: refresh_token,
                grant_type: 'refresh_token',
            }
        };
        const response = await axios.post(
            'https://developer.api.autodesk.com/authentication/v1/authenticate',
             config
        );
        accessToken = await response.data.access_token;
        //set access token as a cookie for session
        res.cookie('access-token', accessToken, { maxAge: 60 * 60 * 1000, httpOnly: true });
        console.log("middleware", response.data);
    }
    next();

});

I also try with headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, but I still get the same error.我也尝试使用标题:{ 'Content-Type': 'application/x-www-form-urlencoded', },但我仍然遇到同样的错误。 (But when I try with curl it's working). (但是当我尝试使用 curl 时,它正在工作)。

What I'm missing?我错过了什么?

try sending payload url-encoded尝试发送有效载荷 url 编码

const params = new URLSearchParams();
params.append('client_id', process.env.FORGE_CLIENT_ID);
// ... other params
axios.post(..., params);

From autodesk docs:来自欧特克文档:

Request Body Structure请求正文结构

The request body is a URL-encoded string of ampersand-concatenated, name-value pairs of the following parameters:请求正文是 URL 编码的字符串,由以下参数组成的与号连接的名称-值对:

I solved this problem by doing this.我通过这样做解决了这个问题。 I have absolutely no idea why it works but if anyone can answer.我完全不知道它为什么会起作用,但如果有人能回答的话。 I find this code confusing.我发现这段代码令人困惑。


            const response = await axios.post(
                'https://developer.api.autodesk.com/authentication/v1/refreshtoken',
                new URLSearchParams({
                    'client_id': process.env.FORGE_CLIENT_ID,
                    'client_secret': process.env.FORGE_CLIENT_SECRET,
                    'grant_type': 'refresh_token',
                    'refresh_token': refresh_token,
                })
            );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM