Facebook Graph API - upload photo using JavaScript

前端 未结 13 1338
温柔的废话
温柔的废话 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:33

    Yes, this is possible, i find 2 solutions how to do that and they are very similar to each other, u need just define url parameter to external image url

    FIRST one using Javascript SDk:

    var imgURL="http://farm4.staticflickr.com/3332/3451193407_b7f047f4b4_o.jpg";//change with your external photo url
    FB.api('/album_id/photos', 'post', {
        message:'photo description',
        url:imgURL        
    }, function(response){
    
        if (!response || response.error) {
            alert('Error occured');
        } else {
            alert('Post ID: ' + response.id);
        }
    
    });
    

    and SECOND one using jQuery Post request and FormData:

     var postMSG="Your message";
     var url='https://graph.facebook.com/albumID/photos?access_token='+accessToken+"&message="+postMSG;
     var imgURL="http://farm4.staticflickr.com/3332/3451193407_b7f047f4b4_o.jpg";//change with your external photo url
     var formData = new FormData();
     formData.append("url",imgURL);
    
      $.ajax({
                        url: url,
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
    
                        success: function(data){
                            alert("POST SUCCESSFUL");
                        }
                    });
    

提交回复
热议问题