分析对手网站北京seo管理
前言
由于业务上的需要,想要在android端实现Google登录。但是android的原生方法受到限制,实现起来比较麻烦。于是想到了一个曲线救国的方法,在android端使用Google的web登录。
实现逻辑
在andorid端拉起外部浏览器,用户登录Google账号,登录成功后,再将登录信息返回到app中。
这里需要注意,必须要跳转到外部浏览器,不能使用android自带的webview。因为google官方已经禁止了这种行为。如下图:
实现步骤
- 创建一个
google-login.html
文件,使用Google OAuth
登录,代码如下
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>Google 登录</title><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>body {background-color: #000;color: #fff;font-family: Arial, sans-serif;display: flex;flex-direction: column;align-items: center;justify-content: center;height: 100vh;margin: 0;}button {padding: 12px 24px;font-size: 16px;border-radius: 8px;border: none;background-color: #4285f4;color: white;cursor: pointer;margin-top: 16px;}#status {margin-top: 20px;font-size: 14px;color: #0f0;}</style>
</head>
<body><h2>使用 Google 登录</h2><button onclick="loginWithGoogle()">登录</button><div id="status"></div><script>const clientId = '你谷歌登录的客户端id';function loginWithGoogle() {const redirectUri = encodeURIComponent('https://www.projectdtest.com/oauth2callback.html');const scope = encodeURIComponent('profile email openid');const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +`client_id=${clientId}&` +`redirect_uri=