rap*_*tor 8 java google-app-engine android google-cloud-endpoints
我正在使用本课程的一个(稍加修改的)解决方法来获取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;
}
Run Code Online (Sandbox Code Playgroud)
虽然我无法获得userId.
INFO: Obtained the userId: null
Run Code Online (Sandbox Code Playgroud)
这种解决方法已经在其他项目中完美运行,因此问题必须在其他地方.我的端点api注释了以下范围,clientIds和audiences:
scopes = {
Constants.EMAIL_SCOPE
},
clientIds = {
Constants.API_EXPLORER_CLIENT_ID,
Constants.WEB_CLIENT_ID,
Constants.ANDROID_CLIENT_ID
},
audiences = {
Constants.ANDROID_AUDIENCE
}
Run Code Online (Sandbox Code Playgroud)
Constants.ANDROID_AUDIENCE并且Constants.WEB_CLIENT_ID是一样的.我没有使用网络客户端,但谷歌告诉我添加一个网络客户端ID.此客户端ID是否需要重定向uris和指定的javascript源?
在我的Android客户端中,我使用以下内容来指定受众.
mCredential = GoogleAccountCredential.usingAudience(
EndpointService.this,
"server:client_id:IDIDIDID.apps.googleusercontent.com"
);
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.
我刚刚明白为什么这个解决方法有效。我需要开始一个新的对象化会话,以便不使用缓存并且可以填充 userId。
Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
632 次 |
| 最近记录: |