Facebook Graph Api Bad Request

时间:2014-08-09 09:57:58

标签: javascript jquery ajax facebook facebook-graph-api

我正在向图表api发出此请求,并从Facebook获得Bad Request响应。我已经改变了几百万次,没有运气。也许我在文档中读错了......

var fd = new FormData();
fd.append("access_token", authToken);
fd.append("source", blob);
fd.append("message", message);

try {

    $.ajax({
        url: "https://graph.facebook.com/me/photos?access_token=" + authToken,
        type: "POST",
        data: fd,
        processData: false,
        contentType: false,
        cache: false,
        success: function (data) {
            console.log("success " + data);

            deferred.resolve(data);
        },
        error: function (shr, status, data) {
            console.log("error " + data + " Status " + shr.status);

            deferred.reject(data);
        },
        complete: function () {
            console.log("Ajax Complete");
        }
    });

} catch (e) {
    alert('error in ajax request ');
    alert(JSON.stringify(e));
}

1 个答案:

答案 0 :(得分:0)

以下是使用phonegap将base64编码的照片直接上传到Facebook的方式:

function postImage(fileURI, message) {

    var deferred = $q.defer();

    var win = function (r) {

        deferred.resolve(r);
    }

    var fail = function (error) {

        deferred.reject(error);
    }

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = 'name_of_photo_' + Math.round((+(new Date()) + Math.random()));
    options.mimeType = "image/jpg";

    var params = new Object();
    params.access_token = "your facebook access token ;)";
    params.message = message;
    params.no_story = false;

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(fileURI, "https://graph.facebook.com/v2.0/me/photos", win, fail, options);

    return deferred.promise;
}

使用文件传输插件。像这样添加:cordova plugin add org.apache.cordova.file-transfer