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
ranjithsabranjithsab 

Problem with Facebook post request

Hi Everybody,

I am trying to integrate salesforce with facebook , and the following is the request i am doing

 

HttpRequest req = new HttpRequest();
req.setEndpoint('https://graph.facebook.com/'+Facebookuserid+'/feed');
req.setHeader('access_token',token);
req.setBody('message='+statusupdate);
req.setMethod('POST');
HttpResponse res = h.send(req);

 

I am getting the error message as "This API call requires a valid app_id."

 

Help me how to make the post request.

 

Thanks in Advance,

Ranjith

 

Vinita_SFDCVinita_SFDC

Hi,

 

Are you getting message like "Error returned from FB(#200) This API call requires a valid app_id."? How are you testing?

 

Also refer: http://stackoverflow.com/questions/6256334/graph-api-posting-to-application-wall

 

 

ranjithsabranjithsab

Hi, 

 

 I given my full code below:

 

public with sharing class Facebook {

public string accessToken { get; set; }
public string clientID = '213658272132885';

public string clientSecret = 'efe8672baab3a0ef1c7025def208042f';
public string code { get; set; }
public string status { get; set; }
public string authuri;

public string statusupdate { get; set; }
public string facebookUname { get; set; }
public string facebookId { get; set; }
public string facebookEmail { get; set; }
public string facebookBio { get; set; }
public list<Facebook__c> fb{get;set;}

public Facebook()
{

}


public PageReference connect()
{
PageReference p = null;

if (ApexPages.currentPage().getUrl() == '/apex/FBPage' ){
String rediruri = 'https://'+ApexPages.currentPage().getHeaders().get('Host')+'/apex/FBPage';
System.debug('host:'+rediruri);

authuri = 'https://graph.facebook.com/oauth/authorize?client_id='+
clientID+'&redirect_uri='+rediruri;
System.debug('URL IS in1at - :'+authuri);
p = new PageReference(authuri);
}

else if(ApexPages.currentPage().getParameters().containsKey('code')) //second req
{
System.debug('URI: '+ApexPages.currentPage().getUrl());

String CodeForFB = ApexPages.currentPage().getParameters().get('code');
System.Debug(' Code ------------ >2 '+CodeForFB);


String rediruri1 = 'https://'+ApexPages.currentPage().getHeaders().get('Host')+'/apex/FBPage';

authuri = 'https://graph.facebook.com/oauth/access_token?client_id='+
clientID+'&redirect_uri='+rediruri1+'&client_secret='+clientSecret+'&code='+CodeForFB+'&scope=read_stream,status_update,manage_pages,user_status,user_notes,publish_actions,offline_access,user_photos';
System.debug('URL IS:'+authuri);

doFinalHttpRequest(authuri);
}
return p;
}
private void doFinalHttpRequest(String uri)
{
Http h = new Http();

HttpRequest req = new HttpRequest();
req.setEndpoint(uri);
req.setMethod('GET');

HttpResponse res = h.send(req);


String resp = res.getBody();
System.debug('FINAL RESP IS:'+resp);
//token string in format of access_token=XXXXX&expires - we want the first value
String accesstoken = '';
if(resp.indexOf('&') > -1){
accesstoken = resp.substring(13, resp.indexOf('&'));
}
else {
accesstoken = resp.substring(13,resp.length());
}

System.debug('Access token:'+accesstoken);
accessToken = accesstoken;

System.Debug(' Access Token -----> '+accessToken);

UserDetail(accesstoken);
}
public void UserDetail(String Url){
Http h = new Http();

HttpRequest req = new HttpRequest();
req.setEndpoint('https://graph.facebook.com/me?access_token='+Url);
req.setMethod('GET');

HttpResponse res = h.send(req);

System.Debug(' Display Detail s---- > '+res.getBody());
String resBody = res.getBody();

String strSpan = 'name":"';
String TempString = resBody.substring(resBody.indexOf(strSpan));
system.Debug(' TempString '+TempString);

facebookUname = TempString.substring(strSpan.length(), TempString.indexOf('",'));
system.Debug(' facebookUname '+facebookUname);

String StrId = 'id":"';
String TempId = resBody.substring(resBody.indexOf(StrId));
facebookId = TempId.substring(StrId.length(), TempId.indexOf('",'));
system.Debug(' facebookId '+facebookId);


fb = new List<Facebook__c>();
fb = [select name,Access_Token__c,FacebookID__c from Facebook__c where name=:Userinfo.getUserId()];
system.debug('fb---'+fb);
if(fb.size()>0){
fb[0].Access_Token__c=Url;
update fb[0];
}
else{
Facebook__c fbn = new Facebook__c();
fbn.Name = Userinfo.getUserId();
fbn.Access_Token__c=Url;
fbn.FacebookID__c=facebookId;
insert fbn;
}
}

public void updatepost(){
string fid;
string token;
Map<String, Facebook__c> map_accesstoken = new Map<String, Facebook__c>();
map_accesstoken = Facebook__c.getAll();

if( map_accesstoken.containsKey( Userinfo.getUserId() )){
Facebook__c fb = map_accesstoken.get( Userinfo.getUserId());
if(fb.FacebookID__c!=null){
fid=fb.FacebookID__c;
token=fb.Access_Token__c;
}
}


Http h = new Http();

HttpRequest req = new HttpRequest();
req.setEndpoint('https://graph.facebook.com/'+fid+'/feed');
req.setHeader('access_token',token);
req.setBody('message='+statusupdate);
req.setMethod('POST');
HttpResponse res = h.send(req);

System.Debug(' resdata--- > '+res.getBody());
}
}

 

ERROR:

{"error":{"message":"(#200) This API call requires a valid app_id.","type":"OAuthException","code":200}}

 

In page one Textarea field is there to give any message and "submit" button is there to display that message in facebook userwall.

 

Thanks

Ranjith

BinayakBinayak

can you plz try to add the access token as a part of the endpoint string not in the header...

 

like

 

req.setEndpoint('https://graph.facebook.com/'+fid+'/feed?access_token=' + token);