17

I forked this google sample project and ran the android signin.

I set up the options in the google developers console and got a client id, but when running the project the id token comes up null.

The complete code is here
This code requests the id token.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_id))
            .requestEmail()
            .build();

Am I missing something in the setup, any suggestions.

Here is the Log

12-04 15:55:59.247 13251-13251/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file.

12-04 15:55:59.247 13251-13251/? I/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file.

12-04 15:55:59.247 13251-13251/? I/SELinux: Function: selinux_android_load_priority , priority version is VE=SEPF_GT-N7100_4.4.2_0033


12-04 15:55:59.247 13251-13251/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
12-04 15:55:59.252 13251-13251/? D/dalvikvm: Late-enabling CheckJNI
12-04 15:55:59.362 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
12-04 15:55:59.362 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve virtual method 194: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
12-04 15:55:59.362 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8
12-04 15:55:59.367 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
12-04 15:55:59.367 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve virtual method 453: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
12-04 15:55:59.367 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
12-04 15:55:59.402 13251-13275/com.google.samples.quickstart.signin I/GMPM: App measurement is starting up
12-04 15:55:59.417 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-04 15:55:59.417 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
12-04 15:55:59.417 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve interface method 15687: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-04 15:55:59.417 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-04 15:55:59.422 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
12-04 15:55:59.422 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve interface method 15691: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-04 15:55:59.422 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve virtual method 528: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin W/dalvikvm: VFY: unable to resolve virtual method 550: Landroid/content/res/TypedArray;.getType (I)I
12-04 15:55:59.462 13251-13251/com.google.samples.quickstart.signin D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-04 15:55:59.512 13251-13251/com.google.samples.quickstart.signin D/AbsListView: Get MotionRecognitionManager
12-04 15:55:59.567 13251-13251/com.google.samples.quickstart.signin D/libEGL: loaded /system/lib/egl/libEGL_mali.so
12-04 15:55:59.567 13251-13251/com.google.samples.quickstart.signin D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
12-04 15:55:59.572 13251-13251/com.google.samples.quickstart.signin D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
12-04 15:55:59.637 13251-13251/com.google.samples.quickstart.signin D/OpenGLRenderer: Enabling debug mode 0
12-04 15:56:12.417 13251-13251/com.google.samples.quickstart.signin D/IdTokenActivity: onActivityResult:GET_TOKEN:success:false
12-04 15:56:48.477 13251-13257/com.google.samples.quickstart.signin D/dalvikvm: GC_FOR_ALLOC freed 707K, 17% free 9803K/11764K, paused 34ms, total 34ms  

Top level build script

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.google.gms:google-services:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

App level build script

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.google.samples.quickstart.signin"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }

    // Resolve dependency differences between app and tests
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:23.1.1'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'

    // Dependency for Google Sign-In
    compile 'com.google.android.gms:play-services-auth:8.3.0'

    // UiAutomatorTesting
    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
}  

google-services.json file

{
  "project_info": {
    "project_id": "se....-la..da-......",
    "project_number": "6043........",
    "name": "Demo"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:6.3....24..:and..id:f62.........40e",
        "client_id": "android:com.example.googlesignindemo",
        "client_type": 1,
        "android_client_info": {
          "package_name": "com.example.googlesignindemo"
        }
      },
      "oauth_client": [
        {
          "client_id": "604...430-uho...slp......v2l........d9...ps.g.....leu........com",
          "client_type": 1,
          "android_info": {
            "package_name": "com.example.googlesignindemo",
            "certificate_hash": "mysha1 fingerprint from debug keystore"
          }
        }
      ],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 1,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 2
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}
Steven
  • 3,812
  • 23
  • 38
JosephK
  • 195
  • 1
  • 1
  • 8
  • Could you post the logs – g90 Dec 04 '15 at 23:41
  • Post your 2 build.gradles files, and are you sure your applicationId in your app the same as the package name created at `http://console.developers.google.com`, the Google's sample project's `com.google.samples.quickstart.signin` – BNK Dec 05 '15 at 01:09
  • @BNK I added the package name 'com.google.samples.quickstart.signin' in the OAuth 2.0 client ID creation page in developers console and added my sha1 debug fingerprint. – JosephK Dec 05 '15 at 01:31
  • Pls open your `google-services.json` file and make sure "package_name" is "com.google.samples.quickstart.signin" – BNK Dec 05 '15 at 01:42
  • Moreover, you can refer to my working sample project at https://github.com/ngocchung/GoogleSignInDemo. Please note that you should customize it with your `applicationId` (`packagename`) and your `google-services.json` file (Pls read `https://developers.google.com/identity/sign-in/android/start-integrating`) – BNK Dec 05 '15 at 01:47
  • The 'package_name' is 'com.google.samples.quickstart.signin', project id and project number also match the info on developer console. – JosephK Dec 05 '15 at 01:49
  • @BNK I tried your project and the google sample project on two different devices and still id token is coming up null and GET_TOKEN:success:false in the log. could you run the app and see if it works on your end. Thanks. – JosephK Dec 07 '15 at 18:26
  • Of course, it works :). Pls wait, I'll post it when I have my PC. – BNK Dec 07 '15 at 21:59
  • Here is the logcat info `12-08 08:28:12.871 28960-28960/com.example.googlesignindemo D/IdTokenActivity: onActivityResult:GET_TOKEN:success:true` `12-08 08:28:12.871 28960-28960/com.example.googlesignindemo D/IdTokenActivity: idToken:eyJhbGciOiJSUzI1NiIsImtpZCI6IjFiZmMzODRlZGZh....VXZnL0FBQUFBQUFBQUFJL0FBQUFBQUF...yIsImxvY2FsZSI6ImVuIiwibmFtZ...akQSGqf-pmGfdFVMAIBjgSV4FfY71ya2-58IU0pQ...JjN3-mWGA` – BNK Dec 08 '15 at 01:27
  • The `OAuth 2.0 client ID` you got, is its Type `Web application` or `Android`? – BNK Dec 08 '15 at 01:34
  • Thanks @BNK for checking. I must have something wrong with the setup but I followed all the steps. The client ID is for android, I will post the google-services.json file with some info blocked out. – JosephK Dec 08 '15 at 01:40
  • Make a new ID and choose its type `Web application`. You can find in `strings.xml` file the following content `` – BNK Dec 08 '15 at 01:43
  • At `https://developers.google.com/identity/sign-in/android/start`, step#, you also find that `Open the Credentials page. Click Add credentials > OAuth 2.0 client ID. Select Web application. Click Create.` – BNK Dec 08 '15 at 01:44
  • I see your .json file, don't use `"client_id": "604...430-uho...slp......v2l........d9...ps.g.....leu........com"` for requestIdToken, use `a valid Web OAuth 2.0 Client ID` as my above comments – BNK Dec 08 '15 at 01:51
  • 1
    Thank you @BNK it works with the web client id. plz submit an answer and i will accept – JosephK Dec 08 '15 at 02:00
  • Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but finally when i retrive tokenId from google api, i get a String tokenId = <857 chars>... ?? If i tryed to validate it googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> i all the get 'error_description": "Invalid Value' ... How are you make a validation of your response? Are you also get String tokenId = <857 chars>? – Sirop4ik Aug 09 '16 at 09:58

3 Answers3

44

As I have commented and as Google's documentation, you should use "Web application" type Client Id, instead of "Android" type Client Id.

As the following screenshot:

BNK's screenshot

BNK
  • 23,994
  • 8
  • 77
  • 87
  • 7
    My app is not a web application, why should I use web client id?! Google is a silly snob... – doctorram Oct 02 '19 at 02:37
  • In my case I'm using web client ID. But still getting error. When I remove this line requestIdToken(getString(R.string.server_client_id)) it works but getIdToken() returns null – Sujith S Manjavana Jun 05 '21 at 09:14
16

Use "default_web_client_id" in GoogleSignInOptions

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

It works perfectly.

BNK
  • 23,994
  • 8
  • 77
  • 87
Praseed M
  • 161
  • 1
  • 4
  • Could you please help me with my issue, i spend some days trying to solve it... I use the same code, but finally when i retrive tokenId from google api, i get a String tokenId = <857 chars>... ?? If i tryed to validate it googleapis.com/oauth2/v3/tokeninfo?access_token= <857 chars response> i all the get 'error_description": "Invalid Value' ... How are you make a validation of your response? Are you also get String tokenId = <857 chars>? – Sirop4ik Aug 09 '16 at 09:57
  • Thanks BNK This helped. Now I'm facing Issue while using firebase auth + Asp Web API here https://stackoverflow.com/q/61878186/7834196 Can you help me BNK ? – parag pawar May 19 '20 at 14:01
0

In my case was just the incompatible version of the libraries... :(

nglauber
  • 18,674
  • 6
  • 70
  • 75