我正在使用来自此 course 的(稍作修改的)解决方法, 以获取 userId,如果请求是从 Android 客户端发送的,则为 null。
/**
* This is an ugly workaround for null userId for Android clients.
*
* @param user A User object injected by the cloud endpoints.
* @return the App Engine userId for the user.
*/
private static String getUserId(User user) {
String userId = user.getUserId();
if (userId == null) {
LOG.info("userId is null, so trying to obtain it from the datastore.");
AppEngineUser appEngineUser = new AppEngineUser(user);
ofy().save().entity(appEngineUser).now();
AppEngineUser savedUser = ofy().load().key(appEngineUser.getKey()).now();
userId = savedUser.getUser().getUserId();
LOG.info("Obtained the userId: " + userId);
}
return userId;
}
虽然我获取不到userId。
INFO: Obtained the userId: null
这个解决方法已经在其他项目中完美运行,所以问题一定出在其他地方。我的端点 api 注释了以下范围、clientIds 和受众:
scopes = {
Constants.EMAIL_SCOPE
},
clientIds = {
Constants.API_EXPLORER_CLIENT_ID,
Constants.WEB_CLIENT_ID,
Constants.ANDROID_CLIENT_ID
},
audiences = {
Constants.ANDROID_AUDIENCE
}
Constants.ANDROID_AUDIENCE
和 Constants.WEB_CLIENT_ID
是一样的。我没有使用网络客户端,而是 Google told me添加网络客户端 ID。此客户端 ID 是否需要指定重定向 URI 和 JavaScript 来源?
在我的 Android 客户端中,我使用以下内容来指定受众。
mCredential = GoogleAccountCredential.usingAudience(
EndpointService.this,
"server:client_id:IDIDIDID.apps.googleusercontent.com"
);
请帮我解决这个问题。
最佳答案
我刚刚明白为什么这个解决方法有效。我需要开始一个新的 objectify session ,以便不使用缓存并且可以填充 userId。
Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load();
关于java - Google App Engine Cloud Endpoints userId 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802603/