Ouath2 401错误:请求缺少必需参数,包含不受支持的参数值,或者格式错误

时间:2017-12-14 08:59:17

标签: node.js oauth-2.0 xmlhttprequest

首先,感谢您抽出时间对此进行审核并提供帮助!其次,我的情况涉及两个应用程序之间的身份验证方案:(1)使用服务器端XHR的Raspberry Pi Node应用程序和(2)使用Oauth 2.0的Ruby API。

Ruby端点存在一些困难,但它们似乎已经稳定下来。足以让我们通过POSTMAN和(希望)从我们的Raspberry Pi中正确验证。

在尝试中,我使用自定义记录器获得以下异常:

NEW EXCEPTION - Thu Dec 14 2017 00:23:20 GMT-0800 (Pacific Standard Time) - Failed RESTful Async POST failed: 4 401 {"UNSENT":0,"OPENED":1,"HEADERS_RECEIVED":2,"LOADING":3,"DONE":4,"readyState":4,"responseText":"{\"error\":\"invalid_request\",\"error_description\":\"The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.\"}","responseXML":"","status":401,"statusText":null,"withCredentials":true}!

以下是通过(编辑)发送的数据:

NEW ENTRY - Thu Dec 14 2017 00:23:19 GMT-0800 (Pacific Standard Time) - REST POST for: http://cms.....ca/oauth/token with body: {"client_id":"...9","client_secret":"......","audience":"....ca","scope":"client admin","grant_type":"password","username":"admin@....ca","password":"qqqqqqq"}

以下POSTMAN POST确实有效(编辑):

enter image description here

POST通过Pi端进行:

headerAsyncPost: (url, data, headerKey, headerValue) => {
        if (c.jobLogging()) l.log(`REST POST for ${url} with body: ${JSON.stringify(data)} ${headerKey} ${headerValue}`, 1);
        return new Promise((res, rej) => {
            try {
                let r = new XMLHttpRequest();
                s.xhrReady(url, r, 'Success', 'RESTful Async POST failed', res, rej);
                r.open("POST", url, true);
                r.setRequestHeader("content-type", "application/json");
                r.setRequestHeader(headerKey, headerValue);
                r.send(JSON.stringify(data));
            } catch (ex) {
                l.err(`Exception ${ex}`);
            }
        });
    },

我不相信这是我的代码(因为它适用于Oauth2的其他网站,目前正在其他地方进行身份验证)但我很感激有任何建议,以帮助澄清这是Pi端还是Ruby-我可以做的一面/任何修复。老实说,我宁愿使用更轻量级和更复杂的Ruby端实现。谢谢!

1 个答案:

答案 0 :(得分:0)

问题已解决 - 端点无法支持带有Body的JSON POST,但只支持附加到URL的参数:

   testPost: (url, params, headerKey, headerValue) => {
        return new Promise((res, rej) => {
            try {
                let r = new XMLHttpRequest();

                for (let i = 0; i < Object.keys(params).length; i++) {
                    if (i === 0) url += `?`;
                    url += `${Object.keys(params)[i]}=${params[Object.keys(params)[i]]}`;
                    if (i < Object.keys(params).length - 1) url += `&`;
                }
                l.log(`${url}`, 1);
                s.xhrReady(url, r, 'Success', 'RESTful Async TestPOST failed', res, rej);
                r.open("POST", url, true);
                r.setRequestHeader(headerKey, headerValue);
                r.send();
            } catch (ex) {
                l.err(`Exception ${ex}`);
            }
        })
    },