通过Node.js服务器上的http请求获取LinkedIn访问令牌

问题描述

我正在关注Authorization Code Flow (3-legged OAuth)文档,现在是第3步,我需要使用授权代码才能从LinkedIn接收访问令牌。在项目中,我使用的是node.js,打字稿和node-fetch库。以下函数创建内容类型为x-www--form-urlencoded的主体,因为这是LinkedIn要求的内容类型。

async function GetAccesstoken(data: any) {

    let body: string | Array<string> = new Array<string>();
    for (let property in data) {
        let encodedKey = encodeURIComponent(property);
        let encodedValue = encodeURIComponent(data[property]);
        body.push(encodedKey + "=" + encodedValue);
    }
    body = body.join("&");

    const response = await fetch("https://www.linkedin.com/oauth/v2/accesstoken",{
        method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},body: body
    }).then((res: any) => {
        console.log("Result",res);
    });
    return response;
}

我没有收到任何错误,响应状态为200,但是我收到的响应值为:

size: 0,timeout: 0,

LinkedIn的承诺是:

access_token
expires_in

当我使用邮递员发布带有参数的url时,请求通过,并且我收到了正确的数据,这表明问题出在我的请求函数而不是我的值之内。

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)