Android Facebook SDK 3.0 auth

我只是一个虾纸丫 提交于 2019-11-30 14:09:12

问题


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.


回答1:


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.




回答2:


Just ran into this same issue. The difference between 64206 (0xface) and 129742 (0x1face) is because FragmentActivity has tacked on an extra 0x10000 to determine which fragment it came from. This was resolved by making sure the super activity was called during onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        // ...
    }
}

You "Can only use lower 16 bits for requestCode", as stated in FragmentActivity.startActivityFromFragment



来源:https://stackoverflow.com/questions/14130481/android-facebook-sdk-3-0-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!