让Facebook登录使用Android Webview

枕上诗书闲处好,门前风景雨来佳。这篇文章主要讲述让Facebook登录使用Android Webview相关的知识,希望能为你提供帮助。
我只是想在android上的WebView上实现facebook登录。问题是我单击html页面上的facebook按钮并在Facebook对话框中插入用户名和密码。网址重定向只是给了我一个黑页。

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WebView webview = new WebView(this); webview.setWebChromeClient(new WebChromeClient()); webview.getSettings().setPluginState(PluginState.ON); webview.getSettings().setjavascriptEnabled(true); webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); webview.getSettings().setjavaScriptCanOpenWindowsAutomatically(true); webview.setWebViewClient(new WebViewClient()); webview.loadUrl("http://peoplehunt.crowdscanner.com/hunt"); setContentView(webview);

这是我的HTML页面上的Facebook常规JavaScript API,当点击facebook按钮时会调用此函数。
$("#login_facebook").click(function() {FB.login(function(response) { //This function should be called if (response.authResponse) { FB.api('/me?fields=name,email,picture,id& type=large', function(response) { //console.log("email "+response.email); $("#submitHandle").hide(); $("#loader").show(); //console.log('Good to see you, ' + response.picture + '.'); var theUsername = response.name; theUsername = theUsername.replace(/ /g, '_')+"_"+response.id+"@meetforeal.com"; //console.log(theUsername); $("input[name=email]").val(encodeURIComponent(response.email)); $("input[name=lastName]").val(encodeURIComponent(response.name)); $("input[name=avatarImage]").val(response.picture); $("input[name=userName]").val(encodeURIComponent(theUsername)); $("#msg_twitter").fadeIn("slow"); $("#submitHandle").show(); $("#loader").hide(); $("#user").attr("action","/crowdmodule/auth/registerattendeefacebook"); $("#user").submit(); }); } else { //console.log('User cancelled login or did not fully authorize.'); } }, {scope: 'email'});

有关如何在Facebook对话框页面上重定向后获得响应的任何想法?谢谢。
答案我在Android应用程序上遇到了同样的问题。问题的原因是FB登录javascript在新窗口中打开一个新页面。然后它尝试关闭它并在登录成功后发回一些javascript auth代码。 WebView通常只是“单一窗口”,因此无法返回,因此空白屏幕。
【让Facebook登录使用Android Webview】请遵循我的工作代码中的流动示例。
< FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0099cc" tools:context=".MyActivity" android:id="@+id/webview_frame"> < WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />


ID“webview”的Webview是我内容的主视图。以下是我的活动代码。
public class MyActivity extends Activity {/* URL saved to be loaded after fb login */ private static final String target_url="http://www.example.com"; private static final String target_url_prefix="www.example.com"; private Context mContext; private WebView mWebview; private WebView mWebviewPop; private FrameLayout mContainer; private long mLastBackPressTime = 0; private Toast mToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_urimalo); // final View controlsView = // findViewById(R.id.fullscreen_content_controls); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); mWebview = (WebView) findViewById(R.id.webview); //mWebviewPop = (WebView) findViewById(R.id.webviewPop); mContainer = (FrameLayout) findViewById(R.id.webview_frame); WebSettings webSettings = mWebview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setAppCacheEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setSupportMultipleWindows(true); mWebview.setWebViewClient(new UriWebViewClient()); mWebview.setWebChromeClient(new UriChromeClient()); mWebview.loadUrl(target_url); mContext=this.getApplicationContext(); }private class UriWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { String host = Uri.parse(url).getHost(); //Log.d("shouldOverrideUrlLoading", url); if (host.equals(target_url_prefix)) { // This is my web site, so do not override; let my WebView load // the page if(mWebviewPop!=null) { mWebviewPop.setVisibility(View.GONE); mContainer.removeView(mWebviewPop); mWebviewPop=null; } return false; }if(host.equals("m.facebook.com")) { return false; } // Otherwise, the link is not for a page on my site, so launch // another Activity that handles URLs Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; }@Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Log.d("onReceivedSslError", "onReceivedSslError"); //super.onReceivedSslError(view, handler, error); } }class UriChromeClient extends WebChromeClient {@Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { mWebviewPop = new WebView(mContext); mWebviewPop.setVerticalScrollBarEnabled(false); mWebviewPop.setHorizontalScrollBarEnabled(false); mWebviewPop.setWebViewClient(new UriWebViewClient()); mWebviewPop.getSettings().setJavaScriptEnabled(true); mWebviewPop.getSettings().setSavePassword(false); mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); mContainer.addView(mWebviewPop); WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; transport.setWebView(mWebviewPop); resultMsg.sendToTarget(); return true; }@Override public void onCloseWindow(WebView window) { Log.d("onCloseWindow", "called"); }} }

这个问题的关键是onCreateWindow。创建一个新窗口并将其插入到框架布局中,并在成功时将其删除。我在shouldOverrideUrlLoading添加了删除。
另一答案这是一个Android项目示例:Github: Android_Popup_Webview_handler_example
这是一个Android Studio项目,展示了如何在Android Webview中处理弹出窗口。大多数开源浏览器不支持打开弹出窗口。
弹出窗口在许多网站(例如,www.feedly.com)中使用的OAuth登录中尤其重要。此项目中的弹出窗口在对话框中打开,可以通过关闭按钮或按Back或者弹出窗口自行关闭(如大多数登录身份验证流程中发生的情况)来解除。
另一答案关于the best answer到this question,您只需要实现您使用的WebViewClient类的onPageFinished方法。
public void onPageFinished(WebView view, String url) { // First, get the URL that Facebook's login button is actually redirecting you to. // It should be something simulator to https://www.facebook.com/dialog/return/arbiter?relation=opener& close=true String webUrl = webView.getUrl(); // Pass it to the LogCat so that you can then use it in the if statement below. Log.d.println(TAG, webUrl); if (url.startsWith("The url that you copied from the LogCat")) { // Check whether the current URL is the URL that Facebook's redirecting you to. // If it is - that's it - do what you want to after the logging process has finished. return; }super.onPageFinished(view, url); }

它对我来说很好。希望它也可以帮助你:)
另一答案在你的shouldOverrideUrlLoading()中覆盖WebViewClient。搜索shouldOverrideUrlLoading here。此外,还有一个参数可以传递给Facebook的登录API; 我认为这是redirect_uri。这应该可以帮助您识别登录成功的时间,并且在shouldOVerrideUrlLoading()中,您只需要检测正在加载的URL,如果它是您指定的redirect_uri,那么只需在该方法中返回true并关闭webview或任何您想要的内容登录成功时。
另一答案以上答案太旧了,它不适用于最新的Facebook sdk 2.7版本。花了4个小时后,我发现它几乎没有变化。以下代码可以与最新的SDK一起使用。
下面提到的是XML布局文件。
< FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0099cc" tools:context=".MainActivity" android:id="@+id/webview_frame"> < WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> < /FrameLayout>

这是Android Activity代码段
public class MainActivity extends AppCompatActivity {private WebView webView; private WebView mWebviewPop; private FrameLayout mContainer; private Context mContext; private String url = "https://www.YourWebsiteAddress.com"; private String target_url_prefix = "www.YourWebsiteAddress.com"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Get outer container mContainer = (FrameLayout) findViewById(R.id.webview_frame); webView = (WebView)findViewById(R.id.webView); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setAppCacheEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatical

    推荐阅读