I am getting
A network error occurred while trying to communicate
with Facebook: Facebook POST failed (HTTP status code 400)
when I
try to deauthorize the user from an app.
I am using restfb1.5.1. I
set the access token and other things correctly. This is how I call .
DefaultFacebookClient facebookClient = new
DefaultFacebookClient(access_token);
boolean result = false;
try {
result = facebookClient.deleteObject(userId);
} catch (com.restfb.FacebookException e) {
e.printStackTrace();
}
Due to facebook, if I don't specify the permission,it will
deauthorizes the app.
Anything that I miss?
Thanks ahead.
Use restfb1.6.6 and Check!!
Best wishes..
Related
what i am trying to achieve should be simple by theory but hell by implementation, if you think otherwise please help.
so what i am trying to achieve here is that I have my facebook page which I created a developer acount for it, and what i want is to take albums from the page to my website.
I am using the latest stable facebook sdk for .net 6.8.0, and i am assuming that the graph api is on v2.2.
this is the code i am using below to get the access token and access my albums
try
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = "---my application number---",
client_secret = "---my application secret---",
grant_type = "client_credentials"
});
var fb2 = new FacebookClient(result.access_token);
dynamic albums = fb2.Get("my application number/albums");
foreach (dynamic albumInfo in albums)
{
try
{
dynamic albumsPhotos = fb2.GetTaskAsync(albumInfo.id + "/photos");
}
catch (Exception Exception)
{
throw Exception;
}
}
}
catch (Exception e)
{
throw e;
}
The data returned in the dynamic albums variables is empty, the thing that confused me more is when i use facebook graph api explorer the call work fine and it return the albums. so what exactly i am missing.
I also read in some threads that you can access your albums without the secret password if they are public but that didn't work for me either i tried it in javascript but no joy.
Thank you in advance.
Since this is your page, it should be pretty easy to retrieve your own photos using Windows PowerShell and http://facebookpsmodule.codeplex.com Get-FBAlbums.
Sorry If there is a post like this question, I checked but I couldn’t find any..
I’m using tank_auth_social for Facebook login.
https://github.com/sicsol/Tank-Auth-Social
Also someone mentioned this problem on issues but no reply..
https://github.com/sicsol/Tank-Auth-Social/issues/2
After user giving access from Facebook at first login it throws exception like this.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/hayvanse/public_html/application/libraries/facebook/base_facebook.php on line 1058
Yeah so we couldn’t have write token it means that yeah? But when I click to login and it works.
The first one only gives exception due to one problem and its not about Facebook token I think.
I’m really newbie on Facebook connect stuff, I updated Facebook and base_facebook library but didn’t work.
I hope had this problem and could help me. This is based function on this line so I wonder if I just skip exception,I'm really confused and looked for it all night :(
protected function throwAPIException($result) {
$e = new FacebookApiException($result);
switch ($e->getType()) {
// OAuth 2.0 Draft 00 style
case 'OAuthException':
// OAuth 2.0 Draft 10 style
case 'invalid_token':
// REST server errors are just Exceptions
case 'Exception':
$message = $e->getMessage();
if ((strpos($message, 'Error validating access token') !== false) ||
(strpos($message, 'Invalid OAuth access token') !== false)) {
$this->setAccessToken(null);
$this->user = 0;
$this->clearAllPersistentData();
}
}
throw $e;
}
Please update base_book with this function , check the link please. Main problem was getuser was returnin 0. You have to update code like blow on base_Facebook file.(latest sdk)
protected function getCode() {
$server_info = array_merge($_GET, $_POST, $_COOKIE);
if (isset($server_info['code'])) {
if ($this->state !== null &&
isset($server_info['state']) &&
$this->state === $server_info['state']) {
// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $server_info['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}
When using CodeIgniter + Facebook PHP SDK: getUser() always returns 0
I am trying to send notification to facebook users via facebook application using restfb api. When tried to publish such notification, it shows an exception
"com.restfb.exception.FacebookGraphException: Received Facebook error response of type
GraphMethodException: Unsupported post request".
Below is my code:
FacebookType = facebookAppAccessToken.publish("user_id/notification?access_token=app_access_token"),
FacebookType.class, Parameter.with("template","send_notification"),
Parameter.with("href,"sample_link"));
Please help me resolving this. Thanks in advance
public void sendNotification(String externalUserId, String message) {
AccessToken appAccessToken = new DefaultFacebookClient()
.obtainAppAccessToken(APP_KEY, APP_SECRET);
FacebookClient facebookClient = new DefaultFacebookClient(
appAccessToken.getAccessToken());
try {
facebookClient.publish(externalUserId
+ "/notifications", FacebookType.class,
Parameter.with("template", message));
} catch (FacebookOAuthException e) {
if (e.getErrorCode() == 200) {
//Not an app user
} else if (e.getErrorCode() == 100) {
//Message cannot be longer than 180 characters
}
}
}
Handle error codes appropriately. And you can add href and ref as per your requirement.
Here are Details of parameters.
I'm using CakePHP 2.3.1 and this plugin: https://github.com/webtechnick/CakePHP-Facebook-Plugin to implement a facebook authentication. I followed this screen cast http://tv.cakephp.org/video/webtechnick/2011/01/12/nick_baker_--_facebook_integration_with_cakephp but it doesn't work, I can't receive the Facebook user information. I mean $this->Connect->user() always return null.
First make sure your user as granted access to your app. Then make a call to retrieve personal details through the api(/me) => $this->Connect->user()
Personally, I use this
public function beforeFilter(){
if($this->Connect->FB->getUser() != 0){
$this->set('facebookUser', $this->Connect->user());
}
Initiating ConnectComponent will populate (or not) the Auth Session. If no id is to be found, redirect user to a login dialog.
Call this method from beforefilter..and save the post data received in some variable like fb_data here.
protected function _valdiateFbRequest() {
if (!isset($this->request->data['signed_request'])) {
// not a valid request from fb
// throw exception or handle however you want
return;
}
$this->signedRequest = $this->request->data['signed_request'];
$this->fb_data=$this->Connect->registrationData(); //save fb data
unset($this->request->data['signed_request']);
if (empty($this->request->data)) {
$this->Security->csrfCheck = false;
}
// validate the request
}
I'm using Facebook Graph Toolkit and doing ASP.NET website development.
There's a Api object in this toolkit and there's this Api.AccessToken that is giving me a headache!
Here goes the problem description:
User visits my website (say, this case is IE 8 browser).
User clicks on Login to FB (in order for my App to retrieve his FB info via the Api graph).
User logins with his FB credential and Approve the App.
User is redirected back to the same page that he clicks on the Login to FB button (in step 2, because the button, will invoke the method RedirectToFacebookAuthorization() ).
User then logs out from Facebook.com (my website hasn't implement any log out button for this scenario).
User has successfully logged out from his FB account, he revisit my website.
My website throws out an exception message as below:
OAuthException : Error validating access token: The session is invalid because the user logged out
The user clicks on Login to FB button on my website but no response (actually, the response is the exception message throwing back again). Even if he opens another IE browser window and click on the button, no response too. I've attempted the try/catch and caught the exception only to inform the user that he has already log out from my website and facebook.
So the situation is how to make the user to obtain another valid Access Token... I can only think of it is that the user has to click on the Login to FB button on my website but the (previous) Access Token is already invalid and cannot be overwritten by the 2nd attempt to login. And the only way that I have tested in order for the user to re-login to my website using his FB is to close the browser Window (but I don't want such user experience because user doesn't know that he has to close the browser and open it again to login to my website).
Even if the user has chosen to go to Facebook.com and login from there, and then revisit my website, he is still not authenticated and not allow to proceed further on my website.
Please enlighten me on what to resolve the above problem. Is the approach that I'm using is wrong?
EDIT: added the codes for further explanation
Here's my code and
protected void Page_PreInit(object sender, EventArgs e)
{
RequireLogin = false;
ExtendedPermissions = "offline_access,user_about_me,user_activities,user_birthday,";
}
protected void btn_fbLogin_Click(object sender, EventArgs e)
{
try
{
if (Api == null)
RedirectToFacebookAuthorization();
}
catch (Exception ex)
{
Response.Write("Failed to login to Facebook.");
}
}
protected void btn_FBMerge_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("getEmail", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter demai = new SqlParameter("#dem", SqlDbType.VarChar, 255);
cmd.Parameters.Add(demai);
cmd.Parameters["#dem"].Value = lbl_FBEmail.Text;
SqlDataReader Dr;
con.Open();
Dr = cmd.ExecuteReader();
if (Dr.Read())
{
if ((lbl_FBEmail.Text == Dr[9].ToString()))
{
// grab each column from DR and place to asp:Labels...
}
else
{
// show some error message
}
}
else
{
// display another division and put the FB email, first name, last name and gender to 4 more labels
}
}
catch (Exception ex)
{
Response.Write("Attempt to Merge Facebook failed");
}
}
protected void btn_Confirm_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("mergeFB", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter dsomeID = new SqlParameter("#someID", SqlDbType.VarChar, 50);
SqlParameter dfbID = new SqlParameter("#fbID", SqlDbType.VarChar, 50);
cmd.Parameters.Add(dsomeID);
cmd.Parameters.Add(dfbID);
cmd.Parameters["#someid"].Value = lbl_MyID.Text;
cmd.Parameters["#fbID"].Value = lbl_FBID.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Your Facebook account has been added to mywebsite account successfully");
}
catch (Exception ex)
{
Response.Write("Attempt to Merge Facebook failed");
}
}
The problem occurs when the button (method is btn_FBMerge_Click) is clicked and IE gives me forever respond and Firefox give me the error that I mentioned earlier.
If I didn't include the offline_access permission, when I click on the btn_FBMerge_Click button, I don't get that strange problem. Then, this still leads to the main problem which is the Api.AccessToken is invalid issue that I mentioned in my original problem.
If the users token is no longer valid, you will need to show the login button to get a new valid session token. If you want to get a token that doesn't expire, you can prompt the user for "offline_access" extended permission. But only do that if its absolutely necessary as people don't tend to like to give apps that permission without good reason.