【问题标题】:GetToken failed with status code: ServiceDisabledGetToken 失败,状态码:ServiceDisabled
【发布时间】:2021-04-09 23:10:05
【问题描述】:

我正在使用 Google 登录登录我正在构建的应用以显示日历。发布 Google 登录 - 它还登录到 Firebase,用户数据存储在实时数据库中。虽然整个应用程序正常工作 - 突然它停止与 Google Calendar API 同步,我在 Info.xml 中收到以下“错误”。我能够登录并且使用新帐户只是没有同步 - 应用程序有效 - 没有数据。我尝试检查多个问题,甚至恢复到旧代码 - 没有差异。 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()
}
}

【问题讨论】:

  • ServiceDisabled 言行一致。
  • 嗨 Martin,好的,有道理,但是当用户能够登录(使用 Google)并查看 Firebase 中的数据时,为什么与 Calendar APi 同步停止工作......或者更确切地说,为什么服务一直已禁用 - 我需要做什么才能反转?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 2016-06-28
    • 2017-07-05
    • 1970-01-01
    • 2018-10-08
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多