开发者

How can I obtain photo id after the photo has been submitted to facebook via graph API?

开发者 https://www.devze.com 2023-03-16 06:51 出处:网络
I am looking for the best way to upload photo to facebook via graph api without any server side process.

I am looking for the best way to upload photo to facebook via graph api without any server side process. The implementation I've made so far is using javascript(jQuery) and HTML form-submit. The code as below:

<form action="" method="POST" enctype="multipart/form-data" target="result">
    Please choose a photo: <input name="source" type="file"><br/>
    Say something about this photo: <input name="message" type="text" value=""><br/>
    <a id="upload-button" href="javascript:;">Upload</a>
</form>

<iframe id="result" src="" name="result" frameborder="1" 
 style="height:75px;" onload="finish_upload(this);"></iframe>

<script>
function finish_upload(iframe){
    //An exception thrown. iframe.contentWindow.document is undefined.
    var content = iframe.contentWindow.document.body.innerHTML;
    console.log(content);
}

$(f开发者_开发百科unction(){
    $('#upload-button').click(function(){
        var access_token = getToken();
        var url = "https://graph.facebook.com/me/photos?access_token=" + access_token;
        $('form').attr('action', url).submit();
    });
});
</script>

After the upload button has been hit. The photo was successfully uploaded and I can saw that a photo id was appeared in result iframe.

{
   "id": "123456789"
}

It seems that since the content of result iframe come from different domain(graph.facebook.com). For some security reasons, I can not obtain the photo id from it by javascript.

The question is that.

Is there any way to obtain iframe content in this case? Or, is there any other way I've missed that can get response from a facebook graph api call more directly?

Thanks.


The only thing I could think of would be to set a specific source url using the source parameter, then retrieve the last 1 or 2 or 5 photos and search for the one with a matching source.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号