GoogleAuthException: unknown when authenticating with Cloud Endpoints

I am trying to add authentication to my cloud endpoints, but I cannot get it to work. I use this blog as a guide: http://devthots.blogspot.nl/2012/07/building-awesome-android-apps-with.html

What I have:

In the AppEngine project:

@Api(name = "noteendpoint", clientIds = { "123456789012-abcdefghijklmnopqrstuvwxyz012345.apps.googleusercontent.com" }, audiences = { "my_project_id.appspot.com" }, namespace = @ApiNamespace(ownerDomain = "example.com", ownerName = "example.com", packagePath = "myapp")) public class NoteEndpoint { // Rest of class, added parameter User to all methods. } 

In my Android app project:

 Note note = new Note(); note.setDescription("Description!"); GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(MainActivity.this, "my_project_id.appspot.com"); credential.setSelectedAccountName(ACCOUNT_NAME); note.setEmailAddress(credential.getSelectedAccountName()); Builder endpointBuilder = new Noteendpoint.Builder( AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential); Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build(); Note result = endpoint.insertNote(note).execute(); // Exception thrown 

When I ran this, a GoogleAuthException: Unknown is GoogleAuthException: Unknown :

 07-08 14:16:45.677: E/AndroidRuntime(27381): Caused by: com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:308) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:854) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.example.test.ui.MainActivity$1.doInBackground(MainActivity.java:131) 07-08 14:16:45.677: E/AndroidRuntime(27381): ... 7 more 07-08 14:16:45.677: E/AndroidRuntime(27381): Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:277) 07-08 14:16:45.677: E/AndroidRuntime(27381): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:301) 

How can i solve this?

The client used for the NoteEndpoint class is copied from the "Client Identifier for Installed Applications", with my debug key sha1.

Using GoogleAccountCredential.usingAudience(MainActivity.this, "server:client_id:my_project_id.appspot.com"); (so with server:client_id: prefixed) does not help.

+6
source share
2 answers

Using

 GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(MainActivity.this, "server:client_id:12312312312-abcdefghijklmnopqrstuvw012345678.apps.googleusercontent.com"); 

and

 @Api(name = "noteendpoint", clientIds = { Ids.WEB_CLIENT_ID, Ids.ANDROID_CLIENT_ID }, audiences = { Ids.ANDROID_AUDIENCE }, namespace = @ApiNamespace(ownerDomain = "example.com", ownerName = "example.com", packagePath = "test")) public class NoteEndpoint { public class Ids { public static final String WEB_CLIENT_ID = "12312312312312-abcdefghijklmnopqrstuvwxyz012345678.apps.googleusercontent.com"; public static final String ANDROID_CLIENT_ID = "12312312312312-0123456789abcdefghabcdefghabcdefgha.apps.googleusercontent.com"; public static final String ANDROID_AUDIENCE = WEB_CLIENT_ID; } 

solved a problem.

+3
source

Be careful when creating your Android client ID. When you enter the package name, use the application identifier written in the application /build.gradle. This may differ from the package for your source classes.

Explanation:

When you create a project, the package name you enter is used by Android as the application identifier and is used to create packages for your source classes. During project development, you can change the package name for your source classes. But be careful, Android Studio does not automatically change your application ID!

+7
source

Source: https://habr.com/ru/post/948942/


All Articles