Facebook的Like按钮重定向至Facebook网站的android [英] Facebook Like button redirecting to facebook site in android

查看:197
本文介绍了Facebook的Like按钮重定向至Facebook网站的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Facebook的喜欢按钮,我application.Here集成是HTML code从developers.facebook.com

I am developing facebook like button to integrate with my application.Here is the html code copied from developers.facebook.com

<html>
<body>                   
    <div id="fb-root"></div>
    <script>
        (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) 
                return;
            js = d.createElement(s); 
            js.id = id;
            js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=my_app_id";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
    <fb:like data-href="http://www.facebook.com/facintegra" data-send="true" data-width="450" data-show-faces="false" data-font="tahoma"/>
</body>

我的Andr​​oid活动code

My android activity code

mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.loadUrl("file:///android_asset/FacebookLikeView.html");

    m_cObjFacebook = new Facebook("Your_id");

    authorizefacebook();

}

private void authorizefacebook(){
    m_cObjFacebook.authorize(this, m_cPermissions, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            m_cAccessToken = values.getString(Facebook.TOKEN);

        }

        @Override
        public void onFacebookError(FacebookError error) {
            System.out.println(error.toString());
        }

        @Override
        public void onError(DialogError e) {
            System.out.println(e.toString());
        }

        @Override
        public void onCancel() {
            System.out.println("Cancel");
        }
    });
}
}

当应用程序启动时,它会检查是否我登录到Facebook或不。如果不是,它会显示Facebook的登录屏幕上登录的,然后后登录成功,它关系到我的Facebook页面,而不是我的Andr​​oid应用程序页面。

When application starts it checks whether I am logged in to the facebook or not. If not, it displays facebook log-in screen to log-in and then after log-in successful, it goes to my facebook page instead of my android app page.

如果它开创我登录的,那么它给人的画面如下图所示。

If it founds me logged in, then it gives the screen as below.

请帮我在哪里,我错了。

Please help me where i am going wrong

我的应用程序的第一个屏幕

First screen of my app

点击后,屏幕上的确定按钮

点击我的web视图,其重定向到链接facebook.com/connect/connect_to_external_page_reload.html等按钮时。请帮我该怎么办?

感谢

推荐答案

我的猜测是,在其中加载FB JS SDK只是没有cookie和web视图使用户没有通过验证。

My guess is that the webview in which you load the fb js sdk just does not have the cookies and so the user is not authenticated.

你是如何验证用户?它采用的SSO(也就是说,它是安装在FB应用程序?)如果是这样的话,那么浏览器(web视图)不知道该用户进行身份验证,当你点击它,它只是试图将您重定向进行身份验证。

How are you authenticating the user? is it using SSO (that is, is the fb app installed?) if that's the case then the browser (webview) is not aware that the user is authenticated and when you click it it just tries to redirect you for authentication.

阅读新的官方博客文章:带来希望移动

Read the new official blog post: Bringing Like to Mobile.

嗯,这看起来完全一样我猜。你看,它说注册看看...意思是JS SDK不承认,用户登录和验证。

Well, this looks exactly as I guessed. You see that it says "Sign up to see..." meaning that the js sdk does not recognize that the user is logged in and authenticated.

您有两个选择,我能想到的:
1.我写了使用新的如同行动,并创建自己的喜欢按钮。
2.当调用 FB.init 通过与你有什么从Android上的 authResponse 参数,看起来是这样的:

You have two options that I can think of:
1. As I wrote use the new Like action and create your own "like button".
2. When calling FB.init pass the authResponse parameter with what you have from android, will look something like:

Java的一部分

m_cObjFacebook = new Facebook("Your_id");
m_cObjFacebook.authorize(this, m_cPermissions, new DialogListener() {
    @Override
    public void onComplete(Bundle values) {
        String response = m_cObjFacebook.request("me");
        JSONObject json = Util.parseJson(response);
        showWebView(json.getString("id"));
    }

    ....
});

private void showWebView(String userid) {
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    StringBuilder url = new StringBuilder("file:///android_asset/FacebookLikeView.html?");
    url.append("token=").append(cObjFacebook.getAccessToken());
    url.append("&expires=").append(cObjFacebook.getAccessExpires());
    url.append("&user=").append(userid);

    mWebView.loadUrl(url.toString());
}

HTML / Javascript的一部分:

Html / Javascript part:

<script type="text/javascript">
    window.fbAsyncInit = function() {   
        var data = {},
            query = window.location.search.substring(1);

        query = query.split("&");
        for (var i = 0; i < query.length; i++) {
            var pair = query[i].split("=");
            data[pair[0]] = pair[1];
        }

        FB.init({
            appId: "YOUR_APP_ID",
            xfbml: true,
            authResponse: data
        });
    };

    // Load the SDK Asynchronously
    (function(d){
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
</script>
<div class="fb-like" data-href="http://www.facebook.com/FacIntegra" data-send="false" data-width="450" data-show-faces="false" data-font="tahoma"></div>

我不是100%肯定这招会的工作,因为在 authResponse signedRequest ,你没有,但它是值得一试的。

I'm not 100% sure that this trick will work, since one of the parameters in the authResponse is the signedRequest which you don't have, but it's worth a try.

在Facebook应用程序(武士刀)设备则验证用它做的是,这意味着不会创建的默认浏览器中的cookies,因此当你打开一个web视图具有类似按钮的JS SDK是不是存在知道你登录,你可以把它想登录到Facebook上的Firefox,然后在打开一个镀铬Facebook和看到你没有登录。

When the facebook application (katana) exists on the device then the authentication is done using it, which means that the cookies on the default browser are not created and so when you open a webview with the like button the js sdk is not aware you are logged in, you can think of it like logging into facebook on firefox, and then open a facebook on chrome and see that you are not logged in.

在FB应用程序未安装使用与web视图,其创建以后可以使用的JS SDK饼干对话框中的SDK验证用户的身份,这就是为什么它工作在这种情况下预期。

When the fb app is not installed the sdk authenticate the user using a dialog with a webview, which creates the cookies that can be later used by the js sdk, that's why it works "as expected" in this scenario.

我在我的第一个编辑给你解决方法尝试将认证数据传递到SDK当它被初始化。
正如我写的话,我不知道它会工作,也许你还需要传递一个签名的请求藏汉,但因为你没有它,你需要创建它,如果你想尝试,只是做这里是怎么描述的完全相反:烧焦的申请,然后将其传递给 FB.init 随着访问令牌和到期参数。

The workaround I gave you in my 1st edit tries to pass the authentication data to the sdk when it's being initialized.
As I wrote then, I'm not sure it will work, maybe you also need to pass it a signed request aswell, but since you don't have it you'll need to create it, if you want to try that just do the exact opposite of what's described here: Singed Request, and then pass it to the FB.init along with the access token and expire parameters.

你有另一种选择是始终用于身份验证对话框,对于阅读:<一href="http://stackoverflow.com/questions/4521013/how-to-disable-facebook-single-sign-on-for-android-facebook-android-sdk">How禁用Facebook的单点登录Android的 - Facebook的 - Android的SDK
。 我建议不要使用该方法,因为它会导致糟糕的用户体验,在移动设备上的所有输入电子邮件地址和密码是不是一个有趣的事情。

Another option you have is to always use the dialog for authentication, for that read this: How to disable Facebook single sign on for android - Facebook-android-sdk.
I advise against using that method since it results in bad user experience, after all typing email and password on mobile devices is not a fun thing to do.

这篇关于Facebook的Like按钮重定向至Facebook网站的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆