Search code examples
androidgoogle-app-engineoauth-2.0google-oauthgoogle-cloud-endpoints

Why do I get INVALID_AUDIENCE using Google Endpoints?


I have followed the HelloEndpoints tutorial by Google where I've implemented the backend API in Java and an Android client, where I'm getting this error from. https://cloud.google.com/appengine/docs/java/endpoints/getstarted/clients/android/

I've deployed the Android app client on a physical device. When I launch the app and touch the "Sign in" button, a dialog is shown since I have two Google accounts on the phone. I choose one and then the correct email address is shown for a couple of seconds in the label before it disappears. Then I'm getting the authentication error.

Several suggested solutions are mentioned at Stackoverflow and other sites , but nothing I've tried so far has helped.

I have tried to:

  • Regenerate the client-id, they are the same on both client- and server side (web client id)
  • The android id have the correct package name at the end of the SHA1
  • Regenerate client libs
  • Update devserver (but the API is also deployed)
  • Shutdown on the web service from the Google Developer Console
  • I've set a project name and email at the so called Consent screen on the API console.
  • Checked so that I'm signing the app with the same debug.keystore that I used when I generated the SHA1. It is used when I build the release.

Kind regards, Ramon


Solution

  • I finally solved the problem yesterday :)

    On Google Developer Console, I realized that I had an Android key for Public API access, but no Android Client ID for OAuth. The only Client ID I had for OAuth was for web applications.

    After I've created one for Android apps, the authentication exception were gone.

    Thereafter I got another exception when I tried to invoke the method requiring the authentication. When I checked the log on Google Developer Console, the invocation wasn't even logged! Which made me think that the invocation of the method isn't correct.

    When I tried to invoke the method from the web it worked. I then checked my terminal and there I saw the [email protected] mail address. So, the invocation from the Android client was sent to my local devserver even though I had the web service deployed (and the servers ip-address wasn't set in the Android code). My next step was to shutdown the local devserver and then I re-deployed the backend API before I tested the Android client again.

    Finally after these two changes I can invoke the authenticated method and get a correct response :)