请求 Google 驱动器范围权限时会提供无限加载对话框
Posted
技术标签:
【中文标题】请求 Google 驱动器范围权限时会提供无限加载对话框【英文标题】:When requesting Google drive scope permission gives infinite loading dialog 【发布时间】:2021-07-24 09:53:24 【问题描述】:我想将 Google Drive 集成到我的应用中,以便在其中存储用户的应用数据。
我的尝试
我已经从 Google dev doc 实现了 Google 登录,并从 additional scopes 部分请求了驱动器范围。我还尝试直接从GoogleSignInOptions.Builder
上的.requestScope()
方法请求范围。
我还在 GCP 中添加了 SHA-1 密钥。
MainActivity.kt
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.Scope
import com.google.android.gms.tasks.Task
import com.google.android.material.textview.MaterialTextView
class MainActivity : AppCompatActivity(), View.OnClickListener
private val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
private lateinit var mGoogleSignInClient: GoogleSignInClient
lateinit var text: MaterialTextView
lateinit var signInBtn: SignInButton
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
text = findViewById(R.id.text)
signInBtn = findViewById(R.id.signInButton)
signInBtn.setOnClickListener(this)
override fun onStart()
super.onStart()
val account = GoogleSignIn.getLastSignedInAccount(this)
updateUI(account)
// Update the UI with user name
private fun updateUI(account: GoogleSignInAccount?)
if (account != null)
text.text = account.displayName
override fun onClick(v: View?)
if (v!!.id == R.id.signInButton)
signIn()
private fun signIn()
val signInIntent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN)
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
else if (RC_DRIVE == requestCode)
Log.d(TAG, "onActivityResult: Permission granted")
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>)
try
val account = completedTask.getResult(ApiException::class.java)
// Signed in successfully, show authenticated UI.
updateUI(account)
// If permission is not granted then it will request it
if (!GoogleSignIn.hasPermissions(
GoogleSignIn.getLastSignedInAccount(this),
Scope("https://www.googleapis.com/auth/drive.appdata")
)
)
// This gives the infinitely loading screen
GoogleSignIn.requestPermissions(
this, RC_DRIVE, GoogleSignIn.getLastSignedInAccount(this),
Scope("https://www.googleapis.com/auth/drive.appdata")
)
catch (e: ApiException)
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.statusCode)
updateUI(null)
companion object
const val RC_SIGN_IN = 100
const val RC_DRIVE = 101
const val TAG = "MainActivity"
【问题讨论】:
【参考方案1】:我必须在 GCP 控制台 > API 和服务 > 凭据中将自己添加为测试用户。
如果您想公开应用,您需要发布该应用。
【讨论】:
以上是关于请求 Google 驱动器范围权限时会提供无限加载对话框的主要内容,如果未能解决你的问题,请参考以下文章
我是不是需要为以下谷歌驱动器范围提交验证请求才能从选择器下载文件?
授予 Simba ODBC 驱动程序或 bigrquery 访问 Google Drive 文件的权限
Google表格中无限真实动态范围平均值的ArrayFormula
Youtube 评论 API 抛出“权限不足:请求的身份验证范围不足”错误
Google Drive API(Driver.Builder)身份验证问题:NullPointerException前置条件检查