Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK 3.0 auth

I'm actually following SessionLoginFragment.java example from facebook sdk samples.

What I really don't understand is this:

when I make

session.openForRead(new Session.OpenRequest(fragment).setCallback(statusCallback));

to log my user to facebook and ask basic read permission (just to test the integration) it simply does not work.

I digged a bit with the debugger and I followed the path. If you don't put a requestCode to the OpenRequest of the session it will give it a random one (and this is ok).

openForRead (my actual session is in CREATED status) will create the permission dialog. When you hit the "Ok" button it will perform a

request.getStartActivityDelegate().startActivityForResult(intent, request.getRequestCode());

You can see the code from the fb sdk source code. Well the requestCode is the same of the session (and here's ok).

When fb log you into the system it will finish his facebook.LoginActivity and call me back my onActivityResult in my activity. The problem is that here the requestCode is different from the request's one. And I don't know why and where it comes from!

If I go into my fb account my application is there, so it means that I've done the correct auth flow that finish well. But I wont get correctly authenticated from my app because of this problem.

Do you know why and how can I solve it?

Thanks.

UPDATE WITH FLOW DETAIL:

This is the actual flow (from fragment):

session.openForRead(new Session.OpenRequest(fragment).setCallback(statusCallback));

After creating it, the request code is (always) 64206 Now openForRead flow will call (final part)

request.getStartActivityDelegate().startActivityForResult(intent, request.getRequestCode());

That call the LoginActivity from facebook SDK and do the client/server validation/oauth

Now on my activity is called onActivityResult (not on the fragment but on the activity part)

and here I call

Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);

And here the requestCode is requestCode 129742

How is it possible? As I already said, the problem in all this flow is that requestCode returned to onActivityResult is different from my pendingRequest requestCode and this break (getActiveSession().onActivityResult return without executing code) the login client part.

like image 298
StErMi Avatar asked Jan 02 '13 22:01

StErMi


1 Answers

I ran into the same problem, except that in my case the offending requestCode was 326350 (0x4face). And I was indeed calling super.onActivityResult, so the workaround proposed by Eric Savage was already in place but not effective. Weirdest thing of all, this stuff did work just a couple of weeks ago, and the offending behaviour appeared without myself upgrading anything (Facebook SDK version, Android version, support library version, even the phone on which I'm developing/testing, are all the same as when I had it working).

However, Eric's answer contains other interesting hints, which I exploited to make my code work again. Basically, instead of passing the whole requestCode to Session.onActivityResult, I cut the lowest 16 bits and pass those only.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session session = Session.getActiveSession();
    int sanitizedRequestCode = requestCode % 0x10000;
    session.onActivityResult(this, sanitizedRequestCode, resultCode, data);
}

I do believe this is a bug that should be fixed in the Facebook SDK, and would insist to have it patched for the next release.

like image 55
Giulio Piancastelli Avatar answered Nov 10 '22 21:11

Giulio Piancastelli