本文介绍了Android + App 引擎:user.getUserID() 在端点为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 GAE 服务器的 Android 应用程序.我尝试按照developers.google.com 上的描述对用户进行身份验证,我将用户参数添加到端点方法等.我得到一个不为空的用户,但此方法getUserId() 返回空值.它类似于这个,相当老的问题:函数 User.getUserId() 在 Cloud 端点 api 中为非空的用户对象返回空但我仍然不知道如何解决它.你如何处理这个错误?你遇到过吗?

I have an Android application with GAE server. I tried to authenticate the user as described on developers.google.com, I added the user parameter to the endpoint methods etc. I get a User which is not null, but this method getUserId() returns null. It is similar to this, rather old problem:Function User.getUserId() in Cloud endpoint api returns null for a user object that is not nullBut I still don't know how to work around it. How do you handle this error? Have you ever encountered it?

在 android 客户端这是我所做的(它的简化):

In android client here's what I did (its simplified) :

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() 给空值,这个用户是否经过身份验证?我读到如果用户对象没有经过身份验证,它应该为空,但我不再确定......我正在使用 App 引擎 SDK 1.8.7.我正在真实设备上进行测试,后端部署到 GAE.

I have also another very important question: is this user authenticated, if getMail() gives me correct account, but getUserId() gives null? I read that user object should be null if it was not authenticated but I am not sure any more...I'm using App engine SDK 1.8.7. I'm testing on a real device and backend deployed to GAE.

推荐答案

我想现在没有好的解决方案.我将电子邮件存储为普通属性并将其从默认提取组中删除,我使用 long 作为主键(由 AppEngine 生成)并通过电子邮件属性查询实体.我不喜欢我的解决方案,如果有人可以提供,我会接受(并实施:))更好的解决方案.

I guess there is no good solution for it right now. I store e-mail as a normal property and remove it from default fetch group, I use long as a primary key (generated by AppEngine) and I query the entity by the e-mail property. I don't like my solution, I'll accept ( and implement :) ) a better one if anyone can provide.

这篇关于Android + App 引擎:user.getUserID() 在端点为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 02:22