0

我正在尝试使用内置工具为我的请求获取 OAuth 2.0 令牌。在阅读文档时这似乎很简单,我将其设置如下: 邮递员请求的屏幕截图

问题是对令牌的请求是使用内容类型发送的application/x-www-form-urlencoded。所以我从服务器得到的响应是415 Unsupported Media Type我看不到将请求内容类型更改为application/json.

postman 控制台日志截图

我是否遗漏了什么或者是创建自定义预请求脚本的唯一方法?

4

1 回答 1

1

https://github.com/oauthjs/node-oauth2-server/issues/92

application/x-www-form-urlencoded ,是 Oauth 支持的内容类型

https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3

如果要创建,可以使用先决条件脚本,例如:

https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

// Refresh the access token and set it into environment variable
pm.sendRequest({
    url:  pm.collectionVariables.get("zoho_accounts_endpoint") + "/oauth/v2/token", 
    method: 'POST',
    header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: {
        mode: 'urlencoded',
        urlencoded: [
            {key: "client_id", value: pm.collectionVariables.get("zoho_client_id"), disabled: false},
            {key: "client_secret", value: pm.collectionVariables.get("zoho_client_secret"), disabled: false},
            {key: "refresh_token", value: pm.collectionVariables.get("zoho_refresh_token"), disabled: false},
            {key: "grant_type", value: 'refresh_token', disabled: false}
        ]
    }
}, function (err, res) {
    pm.collectionVariables.set("zoho_access_token", 'Zoho-oauthtoken ' + res.json().access_token);
});

将其更改为 JSON 或任何您想要的并将值存储到变量并将其用作承载 {{token}}

于 2021-01-06T17:26:12.747 回答