我正在尝试向我的Cloud Endpoints添加身份验证,但我无法让它工作。我正在使用此博客作为指南:http://devthots.blogspot.nl/2012/07/building-awesome-android-apps-with.html
我有什么:
在AppEngine项目中:
@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.
}
在我的Android应用程序项目中:
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
当我运行时,会抛出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)
我该如何解决这个问题?
用于NoteEndpoint
类的clientId是从“已安装应用程序的客户端ID”复制的,其调试密钥为sha1。
使用GoogleAccountCredential.usingAudience(MainActivity.this, "server:client_id:my_project_id.appspot.com");
(以server:client_id:
为前缀)无效。
答案 0 :(得分:7)
生成Android客户端ID时要小心。输入包名称时,请使用app / build.gradle中编写的应用程序ID。它可能与您的源类的包不同。
<强>解释强>
在项目创建时,您输入的包名称由Android用作应用程序ID,用于为源类生成包。在项目开发期间,您可以更改源类的包名称。但要小心,Android Studio不会自动更改您的应用程序ID!
答案 1 :(得分: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;
}
解决了这个问题。