简体   繁体   English

Android Facebook SDK 3.0 auth

[英]Android Facebook SDK 3.0 auth

I'm actually following SessionLoginFragment.java example from facebook sdk samples. 我实际上是在关注facebook sdk示例中的SessionLoginFragment.java示例。

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. 将我的用户登录到Facebook并询问基本读取权限(仅测试集成)它根本不起作用。

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). 如果你没有将requestCode放到会话的OpenRequest中,它会给它一个随机的(这没关系)。

openForRead (my actual session is in CREATED status) will create the permission dialog. openForRead(我的实际会话处于CREATED状态)将创建权限对话框。 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. 您可以从fb sdk源代码中看到代码。 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. 当fb登录系统时,它将完成他的facebook.LoginActivity并在我的活动中回复我的onActivityResult。 The problem is that here the requestCode is different from the request's one. 问题是这里requestCode与请求的不同。 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. 如果我进入我的fb帐户,我的应用程序就在那里,这意味着我已经完成了正确的身份验证流程。 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) 创建它之后,请求代码是(总是) 64206现在openForRead流程将调用(最终部分)

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

That call the LoginActivity from facebook SDK and do the client/server validation/oauth 从facebook SDK调用LoginActivity并执行客户端/服务器验证/ oauth

Now on my activity is called onActivityResult (not on the fragment but on the activity part) 现在,我的活动被称为onActivityResult(不在片段上,而是在活动部分)

and here I call 在这里我打电话

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

And here the requestCode is requestCode 129742 这里requestCode是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. 正如我已经说过的,所有这个流程中的问题是返回onActivityResult的requestCode与pendingRequest requestCode不同,并且这个中断(getActiveSession()。onActivityResult返回而不执行代码)登录客户端部分。

I ran into the same problem, except that in my case the offending requestCode was 326350 ( 0x4face ). 我遇到了同样的问题,除了在我的情况下违规的requestCode3263500x4face )。 And I was indeed calling super.onActivityResult , so the workaround proposed by Eric Savage was already in place but not effective. 我确实称之为super.onActivityResult ,因此Eric Savage提出解决方法已经到位,但效果super.onActivityResult 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). 最奇怪的是,这些东西在几周前就开始工作了,并且在没有自己升级任何东西的情况下出现了违规行为(Facebook SDK版本,Android版本,支持库版本,甚至我正在开发/测试的手机,都是就像我工作时一样)。

However, Eric's answer contains other interesting hints, which I exploited to make my code work again. 然而,Eric的答案包含其他有趣的提示,我利用它来使我的代码再次工作。 Basically, instead of passing the whole requestCode to Session.onActivityResult , I cut the lowest 16 bits and pass those only. 基本上,我没有将整个requestCode传递给Session.onActivityResult ,而是将最低16位切换为仅传递。

@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. 我相信这是一个应该在Facebook SDK中修复的错误,并且会坚持要为下一个版本修补它。

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. 64206(0xface)和129742(0x1face)之间的区别是因为FragmentActivity增加了额外的0x10000来确定它来自哪个片段。 This was resolved by making sure the super activity was called during onActivityResult 通过确保在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 您“只能使用低16位的requestCode”,如FragmentActivity.startActivityFromFragment中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM