I got the error "ErrorReauthorizeFailedReasonSessionClosed" with iOS Facebook SDK - ios

I use facebook sdk on my iOS app to sign-in and sharing story.
Sharing story on Facebook feature was working properly but today it's not working. I don't know why it's not working because no code changes related to that feature.
The followings are the code that requests publish_actions permission.
// Request publish_actions
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound) {
// Permission not granted, tell the user we will not publish
} else {
// Permission granted
}
} else {
NSLog(#"DEBUG: error = %#", error);
// There was an error, handle it
// See https://developers.facebook.com/docs/ios/errors/
}
}];
The error message is as followings,
Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. com.facebook.sdk:ErrorReauthorizeFailedReasonSessionClosed"
UserInfo=0xXXXXXXXXX {
com.facebook.sdk:ErrorLoginFailedReason=
com.facebook.sdk:ErrorReauthorizeFailedReasonSessionClosed,
NSLocalizedFailureReason=
com.facebook.sdk:ErrorReauthorizeFailedReasonSessionClosed,
com.facebook.sdk:ErrorSessionKey= ... >
}
If anybody knows this, please help me.
** Facebook SDK version is 3.18, and publish_actions item is already approved in developer.facebook.com

You can use FBSDKAccessToken
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[self doShare];
} else {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithPublishPermissions:#[#"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:#"publish_actions"]) {
// Do work
[self doShare];
}
}
}];
}
where doShare
-(void) doShare{
NSString *url = [NSString stringWithFormat:#"http://example.com/locations/%d",1];
NSDictionary *properties = #{
#"your action" :url
};
[[[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/example-staging:something"
parameters: properties
HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
// NSLog(#"Post id:%#", result[#"id"]);
}
}];

Related

iOS: Get Facebook Access Token with Auto-Login

I'm using Parse and Facebook for account management in my app. Whenever the user logs in through the facebook button, my code works perfectly because facebook sdk automatically generates a new access token. However, if I use an autologin code which checks whether user has already approved of the app, I have no access token and can't access facebook data for that user. I don't know how to request an access token for a user who has already agreed to use my app.
Loading data:
- (void)_loadData :(BOOL)updateData
{
NSLog(#"entered _loadData");
NSMutableDictionary* userInfoParams = [NSMutableDictionary dictionary];
[userInfoParams setValue:#"id,name,email,gender" forKey:#"fields"];
if([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Expiration date of token: %#", [[FBSDKAccessToken currentAccessToken] expirationDate]);
}
else NSLog(#"No access token");
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:userInfoParams];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection* connection, id result, NSError* error)
{
if(!error)
{
...
}
}
}
Auto-login system:
(void)viewDidAppear:(BOOL)animated
{
if ([PFUser currentUser] || [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]])
{
NSLog(#"Yes");
[self _loadData:YES];
[self transitionToLoginSegue:(id)self];
}
else NSLog(#"No");
}
Also, the access tokens I receive when user clicks through the log in button last for 4 weeks, so if there was a way to manually assign an access token then I could do that, however [FBSDKAccessToken currentAccessToken] is not assignable so I can't do that either.
This is my code below,
if ([self check_network])
{
START_LOAD;
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: #[#"public_profile",#"email",#"user_friends"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
STOP_LOAD;
NSLog(#"Process error");
NSLog(#"%#",error);
TOAST_FOR_TRY_AGAIN;
/*
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Please Try Again"
message:nil
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
*/
} else if (result.isCancelled)
{
STOP_LOAD;
NSLog(#"Cancelled");
} else
{
NSLog(#"Logged in");
NSLog(#"Result=%#",result);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{ #"fields": #"id,first_name,middle_name,last_name,name,picture,email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
NSLog(#"Facebook result=%#",result);
if (!error)
{
[NSUSER setObject:result forKey:#"user_info"];
[NSUSER setObject:[result objectForKey:#"name"] forKey:#"skip_name"];
[NSUSER synchronize];
API_CALL_ALLOC(ls_apicall);
[ls_apicall Login: STRING([result objectForKey:#"id"])];
} else {
STOP_LOAD;
NSLog(#"An error occurred getting friends: %#", [error localizedDescription]);
}
}];
}
}];
}
else
{
TOAST(TOAST_NETWORK_ERROR);
}

how to login with Facebook with preinstalled facebook app in iOS 9

(IBAction)loginWithfacebookClicked:(id)sender
{
// When Clicked on Facebook Button
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
[FBSession openActiveSessionWithReadPermissions:#[#"email",#"user_location",#"user_birthday",#"user_hometown"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
}
Use this logic inside Facebook button click
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
//***Start : requesting for facebook login with valid permissions***
[login logInWithReadPermissions: #[#"public_profile"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
}
else if(result.isCancelled){
NSLog(#"Cancelled");
}
else {
//Successfully logged in
NSLog(#"Logged in");
//dictionary to represent the data to be fetched from facebook
NSDictionary *params = #{ #"fields":#"name,gender,id,picture"};
//Setting up the request parameters
FBSDKGraphRequest *requestForUserProfileDetails = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:params HTTPMethod:#"GET"];
//***Start : requesting for user profile details***
[requestForUserProfileDetails startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection ,id result, NSError* error){
if(!error)
{
//set login type as facebook
//result contains the json response sent by facebook
_socialLoginType = #"facebook";
_name = [result valueForKey:#"name"];
_gender = [result valueForKey:#"gender"];
_socialLoginKey = [result valueForKey:#"id"];
_profileImage = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[result valueForKey:#"picture"] valueForKey:#"data"] valueForKey:#"url"]]]];
}
}];
//***End : requesting for user profile details***
}
}];
//***End : requesting for facebook login with valid permissions***

Login from Facebook App returns nil Token response for iOS

I set Login BehaviourFBSDKLoginBehaviorSystemAccount and try to login, then my app fetch details from Settings of Facebook then its give me successfully response in this class FBSDKLoginManagerLoginResultobject which include token string and user id . But if there is no account added in setting of Facebook then this move user to Facebook App and after authentication when move back to app but this return nil token and no other details. I don't know why this is happening I added some code in following. I hope every one understand my problem those who performed login from Facebook in iOS Development and someone still no then kindly tell me will add more details.
if (![self isCurrentAccessTokenValid] ) {
self.FBLoginManager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
[self.FBLoginManager logInWithReadPermissions:#[ #"email",#"public_profile",#"user_friends"] handler:
^(FBSDKLoginManagerLoginResult *result, NSError *error) {
block(result, error);
NSLog(#"%#",result.token.tokenString);
NSLog(#"%#",result.token.userID);
//NSLog(#"%hhd",result.isCancelled);
[FBSDKAccessToken setCurrentAccessToken:result.token];
}];
} else {
[self fetchUserProfile:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
block(result, error);
}];
}
Looking for helpful response. Thanks.
Try this
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: #[#"public_profile", #"email", #"user_friends"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Logged in");
}
}];

iOS: Login to Facebook through app

I'm trying to login to Facebook through my app using FBSDKLoginManager. I'm asking for some permissions while logging. But every time i get the following error:
[11624:2243947] data----(null)
[11624:2243947] Access Facebook page error:Error Domain=com.facebook.sdk.core Code=8 "The operation couldn’t be completed. (com.facebook.sdk.core error 8.)" UserInfo=0x15eb2e80 {com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Sorry, this feature isn't available right now: An error occurred while processing this request. Please try again later., com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=2, com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0}
Could anyone please help? This is the code that I've written:
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithPublishPermissions:#[#"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
NSLog(#"data----%#",result.grantedPermissions);
if (error) {
// Process error
NSLog(#"Access Facebook page error:%#", error);
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:#"publish_actions"]) {
// Do work
NSLog(#" publish actions permission granted");
[login logInWithReadPermissions:#[#"user_likes",#"user_birthday"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:#"user_birthday"]) {
// Do work
NSLog(#"Permission 2: %#",result.grantedPermissions);
}
}
}];
}
}
}];
Login with Facebook SDK 4.x
Add following code to facebook login button click :
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Error
}
else if (result.isCancelled)
{
// Cancelled
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
[self getFBResult];
}
}
}];
Get Facebook Result Method :
-(void)getFBResult
{
if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"fb user info : %#",result);
}
else
{
NSLog(#"error : %#",error);
}
}];
}
}
You can change the fields of permissions as you want.

Facebook Custom login UI with sdk 4

Im using this code for custom login ui from Facebook developer page, It gave me a log message "FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter". Kindly tell me how can i remove this
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
NSLog(#"%#", result);
if (error)
{
}
else if (result.isCancelled)
{
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
}
}
}];
On using FBLoginManager app gets public_profile permission automatically upon any successful login. You need not request that again. If you need email. Try
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:#"email"]) {
// Do work
}
}
}];

Resources