Facebook Graph API - upload photo using JavaScript

前端 未结 13 1343
温柔的废话
温柔的废话 2020-11-27 11:03

Is it possible to upload a file using the Facebook Graph API using javascript, I feel like I\'m close. I\'m using the following JavaScript

var params = {};
         


        
13条回答
  •  误落风尘
    2020-11-27 11:27

    This still works. I am using it as below:

    var formdata= new FormData();
    if (postAs === 'page'){
        postTo = pageId; //post to page using pageID
    }
    
    formdata.append("access_token", accessToken); //append page access token if to post as page, uAuth|paAuth
    formdata.append("message", photoDescription);
    formdata.append("url", 'http://images/image.png');
    
    try {
        $.ajax({
            url: 'https://graph.facebook.com/'+ postTo +'/photos',
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            cache: false,
            error: function (shr, status, data) {
                console.log("error " + data + " Status " + shr.status);
            },
            complete: function () {
                console.log("Successfully uploaded photo to Facebook");
            }
        });
    } catch (e) {
        console.log(e);
    }
    

    I have to ask though if you people have any idea if this is advisable or has a big security risk compared to using PHP api for Facebook.

提交回复
热议问题