通过谷歌加在WebView登录
问题描述:
我需要在我的android应用程序中使用WebView查看一些网站。而在网站上,可以通过google plus进行登录。一看几个教程和结果我有下面的代码:通过谷歌加在WebView登录
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ua.test.testgoogleauth.MainActivity">
<FrameLayout
android:id="@+id/webview_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</RelativeLayout>
我的活动类:
public class MainActivity extends AppCompatActivity {
private FrameLayout webViewFrame;
private WebView webViewMain;
private WebView webViewWindow;
private final String URL = "https://some_url_with_google_auth";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webViewFrame = (FrameLayout) findViewById(R.id.webview_frame);
webViewMain = (WebView) findViewById(R.id.my_webview);
webViewMain.setWebViewClient(new CustomWebViewClient());
webViewMain.setWebChromeClient(new CustomWebChromeClient());
webViewMain.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webViewMain.setLayerType(View.LAYER_TYPE_HARDWARE, null);
webViewMain.getSettings().setJavaScriptEnabled(true);
webViewMain.getSettings().setDomStorageEnabled(true);
webViewMain.getSettings().setAppCacheEnabled(false);
webViewMain.getSettings().setAllowFileAccess(true);
webViewMain.getSettings().setSupportZoom(true);
webViewMain.getSettings().setSupportMultipleWindows(true);
webViewMain.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webViewMain.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewMain.getSettings().setLoadWithOverviewMode(true);
webViewMain.getSettings().setUseWideViewPort(true);
webViewMain.getSettings().setBuiltInZoomControls(true);
webViewMain.getSettings().setDisplayZoomControls(false);
webViewMain.loadUrl(URL);
}
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
private class CustomWebChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
webViewWindow = new WebView(MainActivity.this);
webViewWindow.setVerticalScrollBarEnabled(false);
webViewWindow.setHorizontalScrollBarEnabled(false);
webViewWindow.setWebViewClient(new CustomWebViewClient());
webViewWindow.getSettings().setJavaScriptEnabled(true);
webViewWindow.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webViewFrame.addView(webViewWindow);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(webViewWindow);
resultMsg.sendToTarget();
return true;
}
@Override
public void onCloseWindow(WebView window) {
webViewFrame.removeView(webViewWindow);
webViewWindow = null;
}
}
@Override
public void onBackPressed() {
if (webViewWindow != null) {
webViewFrame.removeView(webViewWindow);
webViewWindow = null;
} else {
super.onBackPressed();
}
}
}
而且之后一直点击身份验证按钮,我有错误:“错误:disallowed_useragent“。我阅读教程,但没有解决这个问题。 那么,请帮助我,如何解决这个错误?如果我必须使用谷歌加sdk身份验证,如何发送用户令牌到webview?还是有另一种解决方案?
答
如这里在2016年写的: https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html
In the coming months, we will no longer allow OAuth requests to Google in embedded browsers known as “web-views”
所以,你必须使用谷歌标志的API。
+0
嗯......但如果我想使用webview冲浪有些网站使用谷歌身份验证?有没有办法做到这一点?或者,也许我可以使用谷歌身份验证,然后将用户令牌给webview进一步处理? –
我不确定,但我认为您从webivew配置中缺少用户代理。按照这个链接并添加一个合适的用户代理到你的web视图。 https://mobiforge.com/research-analysis/webviews-and-user-agent-strings –
我试图添加许多变体的useragents,但它不工作:( –