我使用GoogleSignIn为 Android 应用程序实现了 Google SSO 。我使用了登录意图和 startActivityForResult 方法:身份验证流程如下所示:
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("client_id")
.requestServerAuthCode("client_id", true)
.requestScopes(Scope("email profile openid https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly"))
.requestEmail()
.build()
binding.googleSignInButton.setOnClickListener{
Timber.i("${gso.scopes}")
val signInIntent = GoogleSignIn.getClient(this, gso)?.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
.....doesn't matter, I tested. never reached here.
}
这种方法在我测试的模拟器设备 API 29 上运行良好。但是,在 API 30,Pixel 3 模拟器上,登录确认屏幕会在短暂加载后重复出现: 在此处输入图像描述 单击“允许”后,屏幕将变成一个小的加载屏幕,并且此确认屏幕将重新出现。
onActivityResult
从未达到。
另外:仅适用于 API 30:在我的 Google 开发人员控制台的 OAuth 同意屏幕中添加 youtube api 范围之前,我遇到了以下行为:
- 第一个确认页面出现了我在 gso 中请求的所有范围
- 我点击允许
- 第二个确认页面仅显示基本范围[openid 电子邮件配置文件](youtube API 范围消失)
- 我点击允许
- 身份验证流程实际上完成了,但现在我只有基本范围。
我还在我的清单中添加了查询:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.example.myapp" />
</intent>
</queries>
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:allowClearUserData="true"
android:name="com.example.myapp.application.YoutubeApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Main Activity-->
<activity
android:name="com.example.myapp.application.activities.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
<!-- Login Activity-->
<activity
android:name="com.example.myapp.application.activities.LoginActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="com.example.myapp"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>