Search code examples
androidgoogle-signingoogle-client

Google signIn tokenId is invalid_token


I need to get google+ signIn tokenId.

Here is my code:

var mGSO = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(WEB_CLIENT_ID)//from developer console
                    .requestEmail()
                    .build()
            mGoogleApiClient = GoogleApiClient.Builder(mActivity)
                    .enableAutoManage(mActivity, this)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, mGSO)
                    .build()

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        var tokenId = result.signInAccount.idToken
    }

So I successfully get tokenId, but when I try to check it here (https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=) I receive message:

{
 "error": "invalid_token",
 "error_description": "Invalid Value"
}

Token the same every time I try to get it! What is happening? Any idea how to fix this?

UPDATE

found this issue: https://github.com/PhilipGarnero/django-rest-framework-social-oauth2/issues/61

I was using the wrong google token from my sign-in on iOS. I originally used user.authentication.idToken which is wrong, and will not work.

The correct token is user.authentication.accessToken.

but i cant find any similar accessToken at GoogleSignInResult object....

UPDATE 2

i am using debug apk. here is my button click code:

fun onGooglePlusClicked(v: View) {
        val signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
        mActivity?.startActivityForResult(signInIntent, GOOGLE_SIGN_IN)
    }

Solution

  • The answer was founded here: https://developers.google.com/identity/protocols/CrossClientAuth

    key words: GoogleAuthUtil.getToken()

    so, here is my updated code:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
    
            Observable.create(Observable.OnSubscribe<String> {
                        var **accessTokent** = GoogleAuthUtil.getToken(mActivity!!, result.signInAccount.account, "oauth2:" + Scopes.PLUS_LOGIN)
                        //send token to server
                    })
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe()
        }
    

    hope this will help someone :)