繁体   English   中英

REST API和Parse.com均无响应云错误:无法对对象进行编码

[英]No Response from REST API, & Parse.com Cloud Error: Can't Form Encode an Object

我正在尝试使用Facebook Graph API将Facebook帖子发布到用户自己的墙上,但是遇到多个问题。

我有一个Wordpress网站,并且正在使用OneAll.com服务来维护和管理用户的社交登录名。 在将帐户链接到Facebook帐户后,我已经添加了将用户发布到我的Facebook应用程序所需的权限。 问题是,OneAll似乎不会更新存储在该社交身份中的访问令牌,直到它过期为止。 但是由于新添加的应用程序权限,访问令牌似乎过早过期,因此我需要在OneAll上手动重新同步用户的社交身份。

它们确实具有调用功能以通过其API( 此处 )同步身份,但是我似乎无法成功向其发送请求。

我试图按照我的Java servlet的要求设置HTTP PUT请求,但是每次调用都会失败,并返回error 411 我已经进行了研究,这似乎是需要在请求中指定内容长度,但是即使我尝试添加内容长度,它似乎也不起作用。

因此,我改为尝试在我的应用程序的Parse.com云代码中设置一个云功能来激活此重新同步,但是该操作每次也会失败,并且出现错误代码,我似乎无法弄清楚:

Input: {"oaIDToken":"924e6f**********"}
Result: Uncaught Error: Can't form encode an Object

知道此错误是什么意思以及如何解决? 我不知道我在做什么错。 这是我的云功能:

Parse.Cloud.define("forceOAIDUpdate", function(request, response) {
var IDToken = request.params.oaIDToken;
var IDURL = "https://w*******d.api.oneall.com/identities/" + IDToken + "/synchronize.json";

Parse.Cloud.httpRequest({
        method: 'PUT',
        url: IDURL,
        body: {
            request: {
                synchronize: {
                    update_user_data: true,
                    force_token_update: true
                }
            }
        },
        success: function(httpResponse) {
            console.log("OA ID token successfully refreshed.");
            console.log(httpResponse.text);
            response.success("OA ID token refreshed");
        },
        error: function(httpResponse) {
            console.error('Requested OA ID refresh failed with response code ' + 
                httpResponse.status);
            response.error("Failed to refresh OA ID. Error: " + 
                httpResponse.data + httpResponse.text + httpResponse.error);
        }
    });
});

我知道一个事实,该函数接收的OneAll身份令牌是有效/正确的,因为它可以在我的其他函数中工作,这些其他函数为用户执行其他操作。 另外,当我尝试在Java servlet中执行请求时,我将基本身份验证登录附加到该请求,但似乎没有什么不同。 无论哪种方式,是否都可能由于缺少身份验证导致此错误? 如果是这样,如何在身份验证云HTTP请求中插入该身份验证标头? 我已经检查过,找不到在线清楚描述它的资源。

我不知道还有另一件事。 我尝试使用REST控制台/客户端手动调用OneAll的REST API,但是无论指向其REST API的任何端口的URL,连接总是失败-控制台立即返回没有响应。 无论是否将基本身份验证标头附加到请求中,这都是正确的。 到底是怎么回事?

可以这么说,我到达了“作家的障碍”,并且用尽了所有的想法来调试它。 我将不胜感激任何帮助。 这个问题使我难过了很多小时。

我终于想通了!!!

Can't form encode an Object解析来自Parse的Can't form encode an Object错误只是意味着我的身体参数需要编码为JSON字符串。

通过在HTTP Request标头中包含基本授权,解决了身份验证问题。 但是它必须在Base64进行编码,并且由于Parse的Cloud代码似乎不支持btoa()函数(正如我尝试过的那样),因此我不得不手动添加/创建Base64编码器。

因此,我将“解析云”功能更改为以下内容,终于起作用了:

Parse.Cloud.define("forceOAIDUpdate", function(request, response) {
                var Base64 = {
            // private property
            _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
            // public method for encoding
            encode : function (input) {
                var output = "";
                var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                var i = 0;
                input = Base64._utf8_encode(input);
                while (i < input.length) {
                    chr1 = input.charCodeAt(i++);
                    chr2 = input.charCodeAt(i++);
                    chr3 = input.charCodeAt(i++);
                    enc1 = chr1 >> 2;
                    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                    enc4 = chr3 & 63;
                    if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                    } else if (isNaN(chr3)) {
                        enc4 = 64;
                    }
                    output = output +
                    Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
                    Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
                }
                return output;
            },
            // public method for decoding
            decode : function (input) {
                var output = "";
                var chr1, chr2, chr3;
                var enc1, enc2, enc3, enc4;
                var i = 0;
                input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
                while (i < input.length) {
                    enc1 = Base64._keyStr.indexOf(input.charAt(i++));
                    enc2 = Base64._keyStr.indexOf(input.charAt(i++));
                    enc3 = Base64._keyStr.indexOf(input.charAt(i++));
                    enc4 = Base64._keyStr.indexOf(input.charAt(i++));
                    chr1 = (enc1 << 2) | (enc2 >> 4);
                    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                    chr3 = ((enc3 & 3) << 6) | enc4;
                    output = output + String.fromCharCode(chr1);
                    if (enc3 != 64) {
                        output = output + String.fromCharCode(chr2);
                    }
                    if (enc4 != 64) {
                        output = output + String.fromCharCode(chr3);
                    }
                }
                output = Base64._utf8_decode(output);
                return output;
            },
            // private method for UTF-8 encoding
            _utf8_encode : function (string) {
                string = string.replace(/\r\n/g,"\n");
                var utftext = "";
                for (var n = 0; n < string.length; n++) {
                    var c = string.charCodeAt(n);
                    if (c < 128) {
                        utftext += String.fromCharCode(c);
                    }
                    else if((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
                    else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
                }
                return utftext;
            },
            // private method for UTF-8 decoding
            _utf8_decode : function (utftext) {
                var string = "";
                var i = 0;
                var c = c1 = c2 = 0;
                while ( i < utftext.length ) {
                    c = utftext.charCodeAt(i);
                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    }
                    else if((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i+1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    }
                    else {
                        c2 = utftext.charCodeAt(i+1);
                        c3 = utftext.charCodeAt(i+2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }
                }
                return string;
            }
            }

var IDToken = request.params.oaIDToken;
var key = "f1a914*******************************";
var secret = "e804************************************";
var basicAuthOA = key + ":" + secret;
var basicAuthOAEncoded = Base64.encode(basicAuthOA);
var IDURL = "https://whentosend.api.oneall.com/identities/" + IDToken + "/synchronize.json";
var headerAuth = {
    'Authorization': "Basic " + basicAuthOAEncoded
}

var params = { 
    request: {
        synchronize: {
            update_user_data: true,
            force_token_update: true
        }
    }
}

Parse.Cloud.httpRequest({
    method: 'PUT',
    url: IDURL,
    headers: headerAuth,
    body: JSON.stringify(params),
    success: function(httpResponse) {
        console.log("OA ID token successfully refreshed.");
        console.log(httpResponse.text);
        response.success("OA ID token refreshed");
    },
    error: function(httpResponse) {
        console.error('Requested OA ID refresh failed with response code ' + 
            httpResponse.status);
        response.error("Failed to refresh OA ID. Error: " + 
            httpResponse.data + httpResponse.text + httpResponse.error);
    }
});
});

我希望这可以帮助其他人节省大量调试时间。 令人沮丧的是,我花了几天的时间才弄清楚这一点,而且我不希望任何程序员都这么认为。

暂无
暂无

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

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