function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravikant kediaRavikant kedia 

How to upload photo on facebook using rest api

Hi all,
 I want to upload photo on facebook using rest api i made a request but i can't success. So how i can upload photo on facebook using rest api  i read  here https://developers.facebook.com/docs/graph-api/reference/v2.1/user/photos and made a request which is given below but i can't success.


            String bodyEncoded = EncodingUtil.base64Encode(file);
             bodyEncoded = bodyEncoded ;
             String boundary = '-------314159265358979323846';
             String delimiter = '\r\n--' + boundary +'\r\n';
             String close_delim = '\r\n--' + boundary;
              String message=''

             String body=boundary +'\r\n'+
            'Content-Disposition:form-data; name="source";filename="'+filename+'"\r\n'+
            'Content-Type: '+filetype+'\r\n'+
             bodyEncoded +'\r\n'+
             boundary;
             String encoded = EncodingUtil.urlEncode(body, 'UTF-8');
        
            System.debug('----------------------------------body is-------------\n'+body);
            HttpRequest req=new HttpRequest();
            req.setMethod('POST');
            req.setbody(body);
            
             req.setHeader('Content-Type', 'multipart/form-data; boundary="'+boundary+'"');
            req.setHeader('Content-length', String.valueOf(body.length()));
            
            req.setheader('Content-type', 'application/x-www-form-urlencoded');
            req.setHeader('Content-length',string.valueof(body.length()) );
             req.setHeader('Content-Encoding','base64');
            req.setTimeout(60000);
            req.setEndpoint('https://graph.facebook.com/v2.0/me/photos?access_token='+accesstoken );
            
            HttpResponse res = null; 
            http h= new Http();
            res= h.send(req);    
            system.debug( '------------upload photo----------------------'+res.getbody());  
    
I used Graph api for it. 
Ravikant kediaRavikant kedia
I got this error :   {"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}