Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the request code constant (64206) for login defined in Facebook SDK

I am using Facebook SDK (4.*) on Android. Just implemented programmatically login (without using "LoginButton") using LoginManager's logInWithReadPermissions(). In order for callback to work I need to call FacebookCallbackManager.onActivityResult(requestCode, resultCode, data); which I do in my activity's onActivityResult method.

However, my onActivityResult handles results form several returning activities and it checks the requestCode to see which activity returned. I see that facebook login returns with 64206 but I am unable to find where that constant is defined. I don't want to hardcode 64206 and I was wondering: does someone knows where is this result code defined in the Facebook SDK (and is it public)?

like image 248
Ognyan Avatar asked Sep 27 '15 08:09

Ognyan


People also ask

What is the Facebook SDK?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.

What is the latest Facebook SDK version?

The current version of the Facebook SDK for Android is version 14.1. 1 and requires the Android API 15. Code and samples for the Facebook SDK for Android are available on GitHub. When you use the Facebook SDK for Android, follow the Facebook Open Source Terms of Use and Privacy Policy.

Is Facebook SDK open source?

This open-source library allows you to integrate Facebook into your Android app. 👋 The SDK team is eager to learn from you!


2 Answers

So after all I decided to dig in with the debugger and found it in the Facebook SDK. Request codes are defined in CallbackManagerImpl.RequestCodeOffset.

You can get the login request code with this: CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode().

There you will find also codes for Share, Message, Like, GameRequest, AppGroupCreate, AppGroupJoin, AppInvite.

like image 89
Ognyan Avatar answered Sep 17 '22 14:09

Ognyan


You shouldn't really have to care about the actual value of Facebook's internally used request code, as the result of CallbackManager.onActivityResult(requestCode, resultCode, data) will tell you whether it was handled or not. That is, first offer the result to the CallbackManager. If it indicates it was handled, you're all done. If it wasn't handled, it is one of your other request code's results, so just continue with the logic you already have in place.

From the docs on CallbackManager:

/**
 * The method that should be called from the Activity's or Fragment's onActivityResult method.
 * @param requestCode The request code that's received by the Activity or Fragment.
 * @param resultCode  The result code that's received by the Activity or Fragment.
 * @param data        The result data that's received by the Activity or Fragment.
 * @return true If the result could be handled.
 */
public boolean onActivityResult(int requestCode, int resultCode, Intent data);

Note the @return remark.

So basically, your code should be structured somewhat like this:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    boolean handled = callbackManager.onActivityResult(requestCode, resultCode, data);
    if (handled) { /* all done */ }
    else { /* result wasn't handled by the callback manager, so check for other potential request codes */ }
}

If you really want, you can dive into the Facebook SDK source to trace the origin of the request code. In particular, refer to CallbackManagerImpl, where the static callbacks are set up with a predefined request code offset.

like image 45
MH. Avatar answered Sep 19 '22 14:09

MH.