7

I'm trying to add authentication to my Cloud Endpoints, but I can't get it to work. I'm using this blog as a guideline: 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 run this, a GoogleAuthException: Unknown is thrown:

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 clientId used for the NoteEndpoint class is copied from "Client id 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) doesn't help.

4

2 回答 2

8

生成 Android 客户端 ID 时要小心。输入包名时,使用app/build.gradle中写的应用ID。它可能与您的源类的包不同。

解释:

在创建项目时,您输入的包名称将被 Android 用作应用程序 ID,并用于为您的源类生成包。在项目开发过程中,您可以更改源类的包名称。但请注意,Android Studio 不会自动更改您的应用程序 ID!

于 2014-11-29T12:39:50.080 回答
3

使用

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

@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;
    }

解决了这个问题。

于 2013-07-08T13:15:29.703 回答