0

How to pass gmail user id and password to webView for Login in Gmail?

Abdul Rizwan
  • 3,904
  • 32
  • 31
  • Actually I want to pass credential into webView for login user automatically – Abdul Rizwan Apr 29 '15 at 10:29
  • Your question is very unclear. Do you want to pass the user's android account's gmail password or do you want to login the user to gmail in your webview and then get it's user id and pass? If you have a data harvesting intention, be advised that it's illegal. – Steel Brain Apr 29 '15 at 10:31
  • User need to save their credential and then when user want they can login to their desired account based on their added credential, I want to know is this possible? because one app is there to save credential and login automatically. "LoginBox" app. – Abdul Rizwan Apr 29 '15 at 10:44

2 Answers2

1

I would add that the load of the javascript function should be done when the html is loaded. To control that, you can use the following:

webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/test.html");

webview.setWebViewClient(new WebViewClient(){

    public void onPageFinished(WebView view, String url){   

         webview.loadUrl("javascript:init('" + theArgumentYouWantToPass + "')");
    }           
});

test.html

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>

<body>
hola
adios
</body>

<script type="text/javascript">

    function init(val){
    // Do whatever you want with your parameter val
    }
</script>
</html>
Kanth K
  • 128
  • 13
0

For Gmail, since gmail went https few months back, there is no simple way. WebView cannot natively handle Basic authentication when using HTTPS connection.

Dont be sad, there is a workaround. You can use the loadUrl function of webview :

   Bundle extras = getIntent().getExtras();            
  mUsrName = extras != null ? extras.getString("username") : null;
  mPassC = extras != null ? extras.getString("passcode") : null;

  mWebView = (WebView) findViewById(R.id.webview);
  mWebView.getSettings().setJavaScriptEnabled(true); 

  mWebView.setWebViewClient(new WebViewClient() {
      @Override  
      public void onReceivedHttpAuthRequest(WebView view,
                                            HttpAuthHandler handler,
                                            String host,
                                            String realm){ 
        handler.proceed(mUsrName, mPassC);
      }  

      public void onReceivedSslError(WebView view,
                                     SslErrorHandler handler,
                                     SslError error) {
        handler.proceed() ;
      } 
    }); 

  String up = mUserName +":" +mPassC;
  String authEncoded = new String(Base64.encodeBase64(up.getBytes()));
  String authHeader = "Basic " +authEncoded;
  Map<String, String> headers = new HashMap<String, String>();
  headers.put("Authorization", authHeader);
  mWebView.loadUrl("https://myhost.com/secured_area", headers);

courtesy : Set credentials on an Android Webview using secured HTTPS connection

Community
  • 1
  • 1
Karan
  • 2,120
  • 15
  • 27