我有一个带有GAE服务器的Android应用程序。我尝试按照developers.google.com上的说明对用户进行身份验证,将user参数添加到终结点方法等。我得到一个不为null的User,但是此方法getUserId()返回null。与此类似,是一个比较老的问题:
Function User.getUserId() in Cloud endpoint api returns null for a user object that is not null
但是我仍然不知道如何解决它。您如何处理此错误?你有没有遇到过?

在android客户端中,这是我的工作(经过简化):

credentials = GoogleAccountCredential.usingAudience(getApplicationContext(),         "server:client_id:" + WEB_CLIENT_ID);
credentials.setSelectedAccountName(accountName);
WarriorEntityEndpoint.Builder endpointBuilder = new WarriorEntityEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credentials);
warriorEntityEndpoint = endpointBuilder.build();

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            warriorEntityEndpoint.getWarrior().execute();
        } catch (Exception e) {
        }
        return null;
    }
}.execute();

在GAE上:
@Api(name = "warriorEntityEndpoint", namespace = @ApiNamespace(ownerDomain = "szpyt.com", ownerName = "szpyt.com", packagePath = "mmorpg.monsters"),
version = "version1",
scopes = {"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
clientIds = {Constants.ANDROID_CLIENT_ID, Constants.WEB_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
public class WarriorEntityEndpoint {
private static final Logger log = Logger.getLogger(WarriorEntityEndpoint.class.getName());
@ApiMethod(name = "getWarrior")
public WarriorEntity getWarrior(User user) throws OAuthRequestException, IOException  {
    log.log(Level.SEVERE, "this gives correct email: " + user.getEmail());
    log.log(Level.SEVERE, "this is null: " + user.getUserId());

我还有另一个非常重要的问题:如果getMail()给我正确的帐户,但getUserId()给空,则此用户是否已通过身份验证?我读到该用户对象如果未通过身份验证,则应该为null,但是我不确定是否还有更多...
我正在使用App Engine SDK 1.8.7。我正在真实设备上进行测试,并将后端部署到GAE。

最佳答案

不久前,我问了同样的问题,并得到了答案。见链接:

Function User.getUserId() in Cloud endpoint api returns null for a user object that is not null

原因是appengine上的错误。

10-07 19:35
查看更多