GetToken 失败,状态码:ServiceDisabled

时间:2021-01-04 05:10:22

标签: android kotlin firebase-authentication google-oauth google-calendar-api

我正在使用 Google Sign in 登录我正在构建的用于显示日历的应用。 Post Google Sign in - 它还会登录 Firebase,用户数据存储在实时数据库中。当整个应用程序正常运行时 - 突然它停止与 Google Calendar API 同步,我在信息中收到以下“错误”。我能够登录并且使用新帐户只是没有同步 - 应用程序有效 - 没有数据。我试过检查多个问题,甚至恢复到旧代码 - 没有差异。 Firebase 用户数据也在应用中显示。任何人都可以提出解决此问题的方法吗?

    2021-01-04 09:59:21.021 2139-4708/com.google.android.gms.persistent W/Auth: [GetToken] GetToken failed with status code: ServiceDisabled
2021-01-04 09:59:21.022 10175-10254/ W/System.err: com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
2021-01-04 09:59:21.022 10175-10254/ W/System.err:     at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:286)
2021-01-04 09:59:21.022 10175-10254/ W/System.err:     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
2021-01-04 09:59:21.023 10175-10254/ W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
2021-01-04 09:59:21.023 10175-10254/ W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
2021-01-04 09:59:21.023 10175-10254/ W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
2021-01-04 09:59:21.023 10175-10254/ W/System.err:     at .Fragments.Home$syncWholeCalendar$1.invokeSuspend(Home.kt:298)

下面是我用来让用户登录的代码

private const val TAG = "WelcomeActivity"

class WelcomeActivity : AppCompatActivity() {

var firebaseUser: FirebaseUser? = null
//For Google Sign In
val RC_SIGN_IN: Int = 9001
private lateinit var mGoogleSignInClient: GoogleSignInClient
lateinit var mGoogleSignInOptions: GoogleSignInOptions
private lateinit var firebaseAuth: FirebaseAuth
private var firebaseUserID : String = ""
private lateinit var refUsers : DatabaseReference

//get data from google signin in handlesigninresult
private var googleId  = ""
private var googleFirstName = ""
private var googleLastName = ""
private var googleEmail = ""
private var googleProfilePicURL = ""

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_welcome)
//        //For google sign in
//        configureGoogleSignIn()
//        setupUI()
    firebaseAuth = FirebaseAuth.getInstance()

    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("896894788293-oe0enptjj2hltdde9isemuf89gtkb7u4.apps.googleusercontent.com")
        .requestEmail()
        .build()
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

    google_login.setOnClickListener {
        signIn()
    }

    login_welcome.setOnClickListener {
        val intent = Intent(this@WelcomeActivity, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }
}

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)
    }
}

private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
    try {
        val account = completedTask.getResult(
            ApiException::class.java
        )
        // Signed in successfully
        googleId = account?.id ?: ""
        Log.i("Google ID", googleId)

        googleFirstName = account?.givenName ?: ""
        Log.i("Google First Name", googleFirstName)

        googleLastName = account?.familyName ?: ""
        Log.i("Google Last Name", googleLastName)

        googleEmail = account?.email ?: ""
        Log.i("Google Email", googleEmail)

        val googleIdToken: String = account?.idToken ?: ""
        Log.i("Google ID Token", googleIdToken)

        googleProfilePicURL = account?.photoUrl.toString()
        Log.i("Google Profile Pic URL", googleProfilePicURL)

        firebaseAuthWithGoogle(googleIdToken)

    } catch (e: ApiException) {
        // Sign in was unsuccessful
        Log.e(
            "failed code=", e.statusCode.toString()
        )
    }
}

private fun firebaseAuthWithGoogle(idToken: String) {
    val credential = GoogleAuthProvider.getCredential(idToken, null)
    firebaseAuth.signInWithCredential(credential)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithCredential:success")

                firebaseUserID = firebaseAuth.currentUser!!.uid
                refUsers = FirebaseDatabase.getInstance().reference.child("Users").child(
                    firebaseUserID
                )
                refUsers.addListenerForSingleValueEvent(object : ValueEventListener {

                    override fun onDataChange(p0: DataSnapshot) {

                        if (p0.exists()) {
                            val user: Users? = p0.getValue(Users::class.java)
                            //Check if user exists in the database
                            if (user!!.getFirstName() != null) {
                                val intent = Intent(
                                    this@WelcomeActivity,
                                    IntroSplashScreen::class.java
                                )
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                                startActivity(intent)
                                finish()
                            } else {
                                val usersHashMap = HashMap<String, Any>()
                                usersHashMap["uid"] = firebaseUserID
                                usersHashMap["firstname"] = googleFirstName
                                usersHashMap["surname"] = googleLastName
                                usersHashMap["profile"] = googleProfilePicURL
                                usersHashMap["primaryEmail"] = googleEmail
                                usersHashMap["search"] =
                                    googleFirstName.toLowerCase(Locale.ROOT)

                                refUsers.updateChildren(usersHashMap)
                                    .addOnCompleteListener {
                                        if (task.isSuccessful) {
                                            val intent = Intent(
                                                this@WelcomeActivity,
                                                IntroSplashScreen::class.java
                                            )
                                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                                            startActivity(intent)
                                            finish()
                                        }

                                    }
                            }
                        }

                    }

                    override fun onCancelled(error: DatabaseError) {
                        TODO("Not yet implemented")
                    }

                })
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInWithCredential:failure", task.exception)
                // ...

            }
            // ...
        }
}

   private fun refreshIdToken() {
        // Attempt to silently refresh the GoogleSignInAccount. If the GoogleSignInAccount
        // already has a valid token this method may complete immediately.
        //
        // If the user has not previously signed in on this device or the sign-in has expired,
        // this asynchronous branch will attempt to sign in the user silently and get a valid
        // ID token. Cross-device single sign on will occur in this branch.
        mGoogleSignInClient.silentSignIn()
            .addOnCompleteListener(
                this
            ) { task -> handleSignInResult(task) }
    }


override fun onStart() {
    super.onStart()
    //Checks if the Google IDToken has expired, if yes it refreshes by SilentSign in and generates new Firebase Token
    refreshIdToken()
    //Checks if user is logged in to firebase
    firebaseUser = FirebaseAuth.getInstance().currentUser
    //If logged in then sends to MainActivity
    if(firebaseUser!=null){
        startActivity(IntroSplashScreen.getLaunchIntent(this))
        finish()
    }
}

override fun onResume() {
    super.onResume()
    refreshIdToken()
}
}

1 个答案:

答案 0 :(得分:0)

需要创建一个 Beta 测试程序,因为任何未关联/列出的人都无法访问数据。