当请求 Google 驱动范围权限时,会出现无限加载对话框
When requesting Google drive scope permission gives infinite loading dialog
我想将 Google 驱动器集成到我的应用程序中,以便在其中存储用户的应用程序数据。
我试过的
我已经从 Google dev doc and requested drive scope from additional scopes 部分实现了 Google 登录。我还尝试直接从 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"
}
}
我必须在 GCP 控制台 > API 和服务 > 凭据中将自己添加为测试用户。
如果您想让应用程序公开可用,您需要发布该应用程序。
我想将 Google 驱动器集成到我的应用程序中,以便在其中存储用户的应用程序数据。
我试过的
我已经从 Google dev doc and requested drive scope from additional scopes 部分实现了 Google 登录。我还尝试直接从 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"
}
}
我必须在 GCP 控制台 > API 和服务 > 凭据中将自己添加为测试用户。
如果您想让应用程序公开可用,您需要发布该应用程序。