{"id":212,"date":"2023-11-18T16:42:37","date_gmt":"2023-11-18T08:42:37","guid":{"rendered":"https:\/\/www.skyblogs.xyz\/?p=212"},"modified":"2023-11-18T16:45:52","modified_gmt":"2023-11-18T08:45:52","slug":"%e5%9c%a8webview%e4%b8%ad%e5%ae%9e%e7%8e%b0h5%e7%9a%84oauth2%e7%99%bb%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.skyblogs.xyz\/index.php\/2023-11-18\/%e5%9c%a8webview%e4%b8%ad%e5%ae%9e%e7%8e%b0h5%e7%9a%84oauth2%e7%99%bb%e5%bd%95\/","title":{"rendered":"\u5728Webview\u4e2d\u5b9e\u73b0H5\u7684Oauth2\u767b\u5f55"},"content":{"rendered":"<p>\u6700\u8fd1\u56e0\u8981\u5c06\u7f51\u7ad9\u5feb\u901f\u8fc1\u79fb\u5230\u5b89\u5353\u7aef\u4e2d\uff0c\u4e8e\u662f\u4fbf\u5f00\u59cb\u4f7f\u7528\u8d77\u4e86webview\uff0c\u867d\u7136\u5927\u4f53\u529f\u80fd\u6b63\u5e38\uff0c\u4f46\u7c7b\u4f3cOauth\u548c\u6587\u4ef6\u4e0a\u4f20\u4e00\u7c7b\u7684\u529f\u80fd\u5374\u83ab\u540d\u5931\u6548<br \/>\n\u6700\u7ec8\u7ecf\u8fc7\u4e24\u5929\u65f6\u95f4\u4e5f\u7b97\u662f\u59a5\u5584\u89e3\u51b3\u4e86\uff0c\u4e8e\u662f\u5199\u4e0b\u6b64\u6587\uff0c\u987a\u4fbf\u505a\u4e00\u4e2a\u8bb0\u5f55<br \/>\n\u5916\u90e8\u73af\u5883\uff1a<strong>Windows10 Android Studio \u591c\u795e\u6a21\u62df\u5668<\/strong><br \/>\n\u5f00\u53d1\u73af\u5883\uff1a<strong>Android12 Api31 Kotlin Gradle<\/strong><\/p>\n<h2 class=\"h-title\">\u601d\u8def\u5206\u6790<\/h2>\n<p>\u6b63\u5e38Oauth2\uff1a<br \/>\n<code>\u7f51\u7ad9\u53d1\u9001\u8bf7\u6c42\u5230\u9a8c\u8bc1\u7aef-&gt;\u9a8c\u8bc1\u7aef\u5411\u56de\u8c03\u5730\u5740\u53d1\u9001code-&gt;\u7f51\u7ad9\u901a\u8fc7code\u8bbf\u95ee\u670d\u52a1\u7aef\u5e76\u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/code><br \/>\n\u8f6c\u8df3\u8def\u5f84\uff1a<br \/>\n<code>\u7f51\u7ad9-&gt;\u7b2c\u4e09\u65b9\u767b\u5f55\u9875\u9762-&gt;\u7f51\u7ad9<\/code><br \/>\n\u800c\u5728\u5b89\u5353\u7684Webview\u4e0b\uff0c\u8fd9\u5957\u903b\u8f91\u4e5f\u4f9d\u7136\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u662f\u53d8\u6210\u4e86\u4ee5\u4e0b\u8fd9\u79cd\u5f62\u5f0f\uff1a<br \/>\n<code>\u7f51\u7ad9(APP)-&gt;\u7b2c\u4e09\u65b9\u767b\u5f55\u9875\u9762-&gt;\u7f51\u7ad9(\u6d4f\u89c8\u5668)<\/code><br \/>\n\u662f\u7684\uff0c\u4eceAPP\u53d1\u9001\u8fc7\u53bb\u7684Oauth2\u8bf7\u6c42\uff0c\u6700\u540e\u56e0\u4e3a\u56de\u8c03\u5730\u5740\u662fhttp\/https\u800c\u8f6c\u8df3\u5230\u4e86\u6d4f\u89c8\u5668\uff0c\u5bfc\u81f4APP\u65e0\u6cd5\u6536\u5230\u56de\u8c03\u5185\u5bb9\u3002<br \/>\n\u597d\u7684\uff0c\u4ee5\u4e0a\u662f\u601d\u8def\u548c\u9519\u8bef\u539f\u56e0\u5206\u6790\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u6765\u5546\u8ba8\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<h2 class=\"h-title\">\u89e3\u51b3\u601d\u8def<\/h2>\n<p>\u8fd9\u91cc\u4f5c\u8005\u6240\u7528\u7684\u4e3aQQ\u7684OAuth2\uff0c\u5177\u4f53\u64cd\u4f5c\u8bf7\u6309\u7167\u81ea\u8eab\u60c5\u51b5\u8c03\u6574<br \/>\n\u5728\u4e0a\u8ff0\u5206\u6790\u540e\uff0c\u6211\u4eec\u7684\u76ee\u6807\u4fbf\u5f88\u660e\u786e\u4e86\uff0c\u5c31\u662f\u8981\u4fee\u6539\u6700\u7ec8\u7684\u56de\u8c03\u5730\u5740\uff0c\u8ba9\u4ed6\u628acode\u4f20\u9012\u5230APP\u4e2d\u800c\u975e\u6d4f\u89c8\u5668\u4e2d\u3002<br \/>\n\u800c\u540c\u65f6\uff0c\u4f5c\u8005\u6b64\u5904\u642d\u5efaAPP\u7aef\u53ea\u662f\u4e3a\u4e86\u6269\u5c55\u751f\u6001\uff0c\u800c\u5e76\u975e\u8fc1\u79fb\u751f\u6001\uff0c\u56e0\u6b64\u4fee\u6539\u5f97\u5728\u4e0d\u5f71\u54cd\u539f\u529f\u80fd\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u3002<br \/>\n\u5373\u76ee\u6807\u7684\u5927\u6982\u6d41\u7a0b\u5982\u4e0b\uff1a<br \/>\n<code>APP-&gt;QQ\u767b\u5f55\u754c\u9762-&gt;\u7f51\u7ad9\uff08\u6d4f\u89c8\u5668\uff09-&gt;APP(schema)<\/code><br \/>\n<em>\u6ce8\uff1a\u6b64\u5904\u901a\u8fc7\u7f51\u9875\u8f6c\u8df3\u56deAPP\u662f\u56e0\u4e3aQQ\u7684OAuth\u4e2d\u56de\u8c03\u5730\u5740\u4e0d\u652f\u6301Schema\u534f\u8bae\uff0c\u82e5\u8bfb\u8005\u7684\u56de\u8c03\u5730\u5740\u652f\u6301Schema\u56de\u8c03\uff0c\u5219\u76f4\u63a5\u901a\u8fc7QQ\u8f6c\u8df3\u56deAPP\u540e\u518d\u8fdb\u884c\u8bf7\u6c42\u91cd\u5199\u5373\u53ef\u3002<\/em><\/p>\n<h3 class=\"h-title\">\u8bf7\u6c42\u4fee\u6539<\/h3>\n<p>\u4e3a\u4e86\u4f7f\u56de\u8c03\u65f6\u80fd\u591f\u5c06\u53c2\u6570\u4f20\u5165APP\uff0c\u6211\u4eec\u9700\u8981\u4fee\u6539OAuth2\u7684\u56de\u8c03\u5730\u5740\uff0c\u800c\u8be5\u56de\u8c03\u5730\u5740\u662f\u5728\u53d1\u9001\u9a8c\u8bc1\u8bf7\u6c42\u65f6\u8fdb\u884c\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u53d1\u9001\u65f6\u5bf9\u8be5\u8bf7\u6c42\u8fdb\u884c\u62e6\u622a\u4e0e\u91cd\u5199<br \/>\n\u5982\uff1a\u539f\u672cwww.example.com\/auth\u5730\u5740\u5c06\u4f1a\u5411https:\/\/graph.qq.com\/oauth2.0\/authorize?response_type=code&amp;client_id=a&amp;redirect_uri=\/auth\/callback&#8221;<br \/>\n\u6b64\u65f6\u6211\u4eec\u5728APP\u4e2d\u5bf9\u8be5\u94fe\u63a5\u8fdb\u884c\u62e6\u622a<\/p>\n<pre class=\"prettyprint linenums\" >  override fun shouldOverrideUrlLoading(\r\n                view: WebView?,\r\n                request: WebResourceRequest?\r\n            ): Boolean {\r\n                val url: String = request?.url.toString()\r\n                    if (url == \"https:\/\/example.com\/auth\" || url == \"https:\/\/example.com\/auth\") {\r\n                        val modifiedUrl: String? = modifyUrl1(url)\r\n                        modifiedUrl?.let { view!!.loadUrl(it) }\r\n                        modifiedUrl?.let { Log.v(\"url result\", it) }\r\n                        return true \/\/ \u8868\u793aURL\u5df2\u7ecf\u88ab\u91cd\u5199\u4e86\r\n                    }\r\n}\r\n    fun modifyUrl1(url: String?): String? {\r\n        return url?.replace(\"auth\", \"auth2\")\r\n    }\r\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u4f20\u5165\u7684auth\u5c31\u88ab\u6539\u5199\u4e3aauth2\uff0c\u5b9e\u73b0\u4e86app\u8def\u7531\u4e0e\u7f51\u7ad9\u8def\u7531\u7684\u5206\u79bb\u3002<br \/>\n\u63a5\u7740\u518d\u4fee\u6539\u7f51\u7ad9\u540e\u7aef\uff0c\u65b0\u589e\u5bf9auth2\u7684\u8def\u7531\u652f\u6301\uff0c\u5e76\u5c06\u65b0\u8def\u7531\u4e2d\u53d1\u9001\u7684Oauth\u7684\u56de\u8c03\u5730\u5740\u4fee\u6539\u4e3aexample.com\/callback2<br \/>\n\u540c\u65f6\u5c06\u8be5\u56de\u8c03\u5730\u5740\u6dfb\u52a0\u5230\u4f60\u7684OAuth\u63d0\u4f9b\u5546\u7684\u56de\u8c03\u5730\u5740\u767d\u540d\u5355\u4e2d<br \/>\n\u4e4b\u540e\u5728callback2\u7f51\u9875\u4e2d\u7f16\u5199\u4ee3\u7801\uff0c\u83b7\u53d6\u4f20\u5165\u7684code\uff0c\u4e4b\u540e\u5c06\u83b7\u53d6\u7684code\u62fc\u63a5\u8fdb\u94fe\u63a5\u5b57\u7b26\u4e32\u3002\u5982\uff1aMyapp:\/\/homepage\/auth?code=xxxxxxxxxx<br \/>\n\u540c\u65f6\u901a\u8fc7Window.location\u5b9e\u73b0schema\u7684\u8f6c\u8df3\uff0c\u8fd9\u6837code\u4fbf\u8fdb\u5165\u4e86APP\u4e2d<\/p>\n<h3 class=\"h-title\">\u622a\u53d6\u8f6c\u8df3<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728APP\u4e2d\u622a\u53d6\u7531\u5916\u90e8schema\u8f6c\u8df3\u8fdbAPP\u7684\u5185\u5bb9\uff0c\u540c\u65f6\u5c06\u8bf7\u6c42\u4e2d\u643a\u5e26\u7684\u5185\u5bb9\u53d1\u5f80\u7f51\u7ad9\u7684\u9a8c\u8bc1\u5730\u5740<\/p>\n<pre class=\"prettyprint linenums\" >    override fun onNewIntent(intent: Intent?) {\r\n        super.onNewIntent(intent)\r\n        handleIntent(intent)\r\n    }\r\n\r\n    private fun handleIntent(intent: Intent?) {\r\n     val data = intent.data\r\n        val schemaUri = data.toString()\r\n        if (schemaUri.contains(\"type=auth\")) {       \/\/\u5f53\u94fe\u63a5\u5305\u542bauth\u65f6 \u5c1d\u8bd5\u63d0\u53d6code\u5e76loadurl\r\n            val pattern = Pattern.compile(\"(code=.*?)(&amp;|$)\")\r\n            val matcher = pattern.matcher(schemaUri)\r\n            if (matcher.find()) {\r\n                val code: String? = matcher.group(1)\r\n                webView?.loadUrl(\"$WEB_URL\/callback?$code\")\r\n                return\r\n            }\r\n        }\r\n        webView?.loadUrl(WEB_URL)\r\n        return\r\n}\r\n<\/pre>\n<p>\u6b64\u65f6\uff0c\u7531\u5916\u90e8\u8c03\u7528APP\u65f6\u53d1\u9001\u8fdb\u6765\u7684code\u5c31\u4f1a\u88ab\u91cd\u5b9a\u5411\u5230\u7f51\u7ad9\u7684callback\u5730\u5740\uff0c\u8fd9\u6837\u5c31\u53c8\u56de\u5230\u4e86\u539f\u672c\u7684\u9a8c\u8bc1\u6d41\u7a0b\uff0c\u540c\u65f6\u5728APP\u4e2d\u5b8c\u6210\u4e86\u767b\u5f55\u6821\u9a8c\u3002<\/p>\n<h3 class=\"h-title\">\u603b\u7ed3<\/h3>\n<p>\u521b\u9020\u51fa\u4e00\u6761\u65b0\u7684OAuth\u8def\u5f84\uff0c\u5e76\u5728\u521d\u6b21\u53d1\u9001Oauth\u8bf7\u6c42\u65f6\u76f4\u63a5\u6539\u5199\u539f\u8bf7\u6c42\u4f7f\u5176\u91cd\u5b9a\u5411\u5230APP\u4e13\u7528\u7684\u8def\u7ebf\u4e0a\uff0c\u968f\u540e\u501f\u7528\u6d4f\u89c8\u5668\u4f5c\u4e3a\u8f6c\u8df3\u4e2d\u4ecb\uff0c\u5c06OAuth\u56de\u8c03\u7684Code\u62e6\u622a\u5e76\u53d1\u5f80APP\u3002<br \/>\n\u4ee5\u4e0a\uff0c\u672c\u968f\u7b14\u5230\u6b64\u7ed3\u675f\uff0c\u4e0b\u4e00\u7bc7\u5e94\u8be5\u662f\u89e3\u51b3Webview\u5728API26~API31\u7684\u9ad8\u7248\u672c\u4e0b\u6587\u4ef6\u4e0a\u4f20\u7684\u95ee\u9898\u5427<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u56e0\u8981\u5c06\u7f51\u7ad9\u5feb\u901f\u8fc1\u79fb\u5230\u5b89\u5353\u7aef\u4e2d\uff0c\u4e8e\u662f\u4fbf\u5f00\u59cb\u4f7f\u7528\u8d77\u4e86webview\uff0c\u867d\u7136\u5927\u4f53\u529f\u80fd\u6b63\u5e38\uff0c\u4f46\u7c7b\u4f3cOauth\u548c\u6587\u4ef6\u4e0a\u4f20\u4e00\u7c7b\u7684\u529f\u80fd\u5374\u83ab\u540d\u5931\u6548 \u6700\u7ec8\u7ecf\u8fc7\u4e24\u5929\u65f6\u95f4\u4e5f\u7b97\u662f\u59a5\u5584\u89e3\u51b3\u4e86\uff0c\u4e8e\u662f\u5199\u4e0b\u6b64\u6587\uff0c\u987a\u4fbf\u505a\u4e00\u4e2a\u8bb0\u5f55 \u5916\u90e8\u73af\u5883\uff1aWindows10 Android Studio \u591c\u795e\u6a21\u62df\u5668 \u5f00\u53d1\u73af\u5883\uff1aAndroid12 Api31 Kotlin Gradle \u601d\u8def\u5206\u6790 \u6b63\u5e38Oauth2\uff1a \u7f51\u7ad9\u53d1\u9001\u8bf7\u6c42\u5230\u9a8c\u8bc1\u7aef-&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":222,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"Webview,OAuth,\u670d\u52a1\u8fc1\u79fb","_seopress_robots_index":"","footnotes":""},"categories":[6],"tags":[42,40,41],"class_list":["post-212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-teck-discussion","tag-oauth2","tag-webview","tag-41"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":9,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/posts\/212\/revisions\/255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/media\/222"}],"wp:attachment":[{"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyblogs.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}