restfb api not support when user logout from facebook & access key generate every time - facebook

I am using RESTFb api to post a message on facebook wall
my code is:
val facebookClient: FacebookClient = new DefaultFacebookClient("access_key")
def publishMessage(msg:Mesage): String = {
val publishMessageResponse: FacebookType = facebookClient.publish("me/feed", classOf[FacebookType],
Parameter.`with`("message", msg))
publishMessageResponse.getId()
}
But this code is working only when I am login in my facebook account.If I am not login it give me the error of "user session logout". and it told me to generate the access token every time.

Thats because you are trying to post to your own Wall.. thats why, OAuth needs to validate you are logged otherwise it wont post anything... if you where posting to a PAGE... well that's diferent you could use APP and SECRET ID's to generate AccessTokens on your website.

Related

Facebook: Can't publish on user feed in test mode

I have developed a Facebook app in test mod and I want to publish on the test user's wall.
this is the link where the user register and accept the permissions:
https://www.facebook.com/dialog/oauth?type=user_agent&client_id=MY_APP_ID&display=popup&redirect_uri=THE_URL&scope=email%2Cpublish_actions
After that I am getting an some sort of a token, using this token I am doing the following request to the graph:
/oauth/access_token?client_id=MY_APP_ID&client_secret=MY_APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=".$token
the token coming back is now being used by me to get the user's details from the graph:
/me/?$user_access_token&fields=name,age_range,id,email,gender,currency,locale,timezone"
I am getting in result all the fields listed above. (one strange thing, the id coming back is not the test user's public id, the other field are correct...)
when I want to post so someones wall I am running this int the graph:
/oauth/access_token?client_id=MY_APP_ID&client_secret=MY_SECRET&grant_type=client_credentials
the token coming back will be used here:
$token = self::getAccessToken($app_id,$app_secret);
$post_array = array();
$post_array['access_token'] = $token;
$post_array['message'] = urlencode($message);
$post_array['link'] = urlencode($link);
$res = self::post("https://graph.facebook.com/".$user_id."/feed?",$post_array);// the user id might be wrong?
and I am getting the following error:
{"error":{"message":"(#200) Permissions error","type":"OAuthException","code":200,"fbtrace_id":"EO5WvMfYYgC"}}
What can it be?
the fact that I am in test mod?
or maybe the user's ID i got back is wrong?
Thanks
The ID is correct, you only get an "App Scoped ID". You are not supposed to use the global/public ID anymore, and you should use /me/feed for posting, not /user-id/feed. You can only post on the wall of the authorized user anyway.

How do I remove the Facebook login prompt when displaying images pulled with graph api on a page?

I recently started playing around with Facebook graph API and wanted to integrate images pulled from my Facebook page to display on my website. I have the images displaying properly but my problem is whenever anyone tries to view the page they are asked to log into Facebook first. Is there any way to display the images without prompting the user to log into Facebook?
Here is what I am using to make the session:
$app_id = 'id';
$app_secret = 'secret';
$redirect = 'my webpage';
// init app with app id and secret
FacebookSession::setDefaultApplication($app_id,$app_secret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($redirect);
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
Any help would be appreciated.
Step one: familiarize yourself with the API. Read the docs, and play around with the Graph API Explorer
Step two: the code you provided has nothing to do with what you are trying to achieve (displaying page photos). That code is basically the getting started code used in the docs. If you need help, post the relevant code.
Step three: as mentioned by #CBroe, authenticating the visitor is not needed to display photos from a page. What you might want to explore:
The page admin authenticating your app with the right permissions (maybe manage_pages)
with the user access token you just got, you extend it to long-lived one
then you query the API to get a page access token that won't expire
you store this access token and query the API to get the relevant data and store it (GET /{PAGE-ID}/photos or GET /{PAGE-ID}/albums ... etc)
you show the stored data to your visitors
Notes:
Do not make these calls on client-side ... i.e. reveal your page access token, since you can do this in the backend.
Use the realtime updates to get notified when you should query the API and get new photos instead of periodically querying the API to pull new photos, or even worst, querying the API on each user visit.

facebook api call from multiple pages

I'm creating a facebook app with facebook php adk 3.0.
I was able to do facebook connect login successfully as per the sample code from github.
the page to which i'm redirecting after facebook login is returning my correct user id using the function
$user = $facebook->getUser();
But if i go to any other page in the app, and if i call $facebook->getUser();, it's returning zero.
Seems like i should save logged in user session some where, anybody know solution for this?
After long search i found solution for this.
From the page to which u'll be redirected after facebook login, store access token in session.
$access_token = $facebook->getAccessToken();
$_SESSION['token'] = $access_token;
Then if you want to call a fb api from any other page, set access token as below before calling the api.
$access_token = $_SESSION['token'];
$facebook->setAccessToken($access_token);
Hope it will help.

Facebook C# sdk - Does the FacebookApp.Get("me") access to Facebook or just to cookie?

I'm using the Facebook c# sdk on a new project and i've a question.
In the follow code:
var app = new FacebookApp();
app.AppId = "myappid";
app.AppSecret = "theappsecretcode";
if (app.Session != null)
{
dynamic me = app.Get("me");
}
When i'm calling the method Get of FacebookApp, i'm i just accessing to the Cookie saved when i've authenticated with Facebook or i'm connecting to the Facebook and getting the values?
I'm asking because i'm saving the values of the user in a session when i authenticate with the Facebook, but if the method Get doesn't access to Facebook and just to the cookie, then i'll stop using the session and start to access to the Facebook cookie instead (i haven't understand yet if the cookie save some information about the user, but i've searched in "Watch" window in VS and i haven't found anything)
Thanks
I've examined source code of FacebookApp and FacebookAppbase classes and have found out that method Get is always accessing the Facebook (there is no any cache).

Could not authenticate with OAuth

I am using Tweetsharp and I am trying to play with the Twitter Application. At present it is a simple console application.
I have searched in the net and found some articles where most of them are stating that after August 16th 2010, the basic authentication for the twitter is no longer applicable. Instead OAuth has come into place.
Henceforth, I have gone to the Twitter Apps and created one for me.(since it's a desktop application, so I choose Application Type to be Client and not browser.)
These are the various information that I got
Consumer key : NxDgjunKLu65CW38Ea1RT
Consumer secret :JOomsRGPTHct9hFjGQOTpxScZwI5K8zkIpOC1ytfo
Request token URL : https://twitter.com/oauth/request_token
Access token URL : https://twitter.com/oauth/access_token
Authorize URL: https://twitter.com/oauth/authorize
As a very basic step what have planned is that, I will write/post some tweet something to my wall.
Henceforth, I made the following(some code has been taken from web as I was using those as a reference)
string consumerKey = "NxDgjunKLu65CW38Ea1RT";
string consumerSecret = "JOomsRGPTHct9hFjGQOTpxScZwI5K8zkIpOC1ytfo";
FluentTwitter.SetClientInfo(new TwitterClientInfo { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret });
//Gets the token
var RequestToken = FluentTwitter.CreateRequest().Authentication.GetRequestToken().Request().AsToken();
var twitter = FluentTwitter.CreateRequest()
.AuthenticateWith(
consumerKey
,consumerSecret,
RequestToken.Token,
RequestToken.TokenSecret)
.Statuses().Update("I am writing my first tweets").AsXml();
var response = twitter.Request();
var status = response.AsStatus();
But the response is
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<error>Could not authenticate with OAuth.</error>
<request>/1/statuses/update.xml</request>
</hash>
I am trying for a long time to understand the problem but all in vain.
I need help.
Thanks
Getting the request token is only the first step of the OAuth process. You need to get the request token, authorize the token, and then trade if for an access token. You then use the access token to send a tweet.
See this link for a flowchart of the full OAuth process.