[Android] OAUTH using Webview

Source: Internet
Author: User
Tags oauth

1. Source
Recently, Google login requires that Google Play Service be followed on the mobile phone. Some mobile phones, such as Xiaomi, do not use Google Play Servcie. Therefore, it is necessary to implement a webview login method. Google redirects are configured by the Server. We only need to request a URL to jump to the Google authorization page. After authorization, the Server jumps to the Server where the Server obtains user information, then, return the user information to an address. You only need to parse the returned page results. 2. Code Implementation
Permission<Uses-permission android: name = "android. permission. INTERNET"/> Clear cookieTry {CookieSyncManager. createInstance (this); CookieManager. getInstance (). removeAllCookie ();} catch (Exception e) {Log. d ("Google Login", "Clear cookie failed ");} Final Page DetectionWebView webview = (WebView) findViewById (R. id. login); webview. setWebViewClient (new WebViewClient () {@ Override public void onPageFinished (WebView view, String url) {if (url. contains (succeedUrl) Log. d ("Google Login", "onPageFinished, url =" + url) ;}}); webview. loadUrl (requestUrl ); To obtain the page content, you must use JS to call JAVA code.Class Handler {@ JavascriptInterface // This is required for api level17. If this parameter is not added, no error is reported, that is, no call is required. It took one and a half hours to complete. Public void show (String data) {Log. d ("Google Login", "handler, show, data =" + data); System. out. println ("data =" + data) ;}} webview. getSettings (). setJavaScriptEnabled (true); // sets the webview available for JS. addJavascriptInterface (new Handler (), "handler"); // register the Handler object and name it handler public void onPageFinished (WebView view, String url) {if (url. contains (succeedUrl) Log. d ("Google Login", "onPageFinished, url =" + url); view. loadUrl ("javascript: window. handler. show (document. body. innerHTML );");}}); 3. ReferencesClear cookie http://developer.android.com/reference/android/webkit/CookieSyncManager.html how to get the content http://veikr.com/201106/android_webview_content-html.html in webView in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.