cloudBackend.setCredential没有设置CloudEntity的createdBy / updatedBy /所有者性质 [英] cloudBackend.setCredential not setting createdBy/updatedBy/owner properties of CloudEntity

查看:235
本文介绍了cloudBackend.setCredential没有设置CloudEntity的createdBy / updatedBy /所有者性质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用手机后台启动,我试图使用确保按ID设置时更新实体。我不断收到错误

  com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权
    {
    code:401,
    错误:
    {
    域:全局,
    位置:授权,
    的locationType:头,
    消息:用于更新CloudEntity许可数量不足:CE:123456方式:USER:123456,
    原因:要求
    }
    ]
    消息:用于更新CloudEntity许可数量不足:CE:123456方式:USER:123456
    }

文档(的https://cloud.google.com/developers/articles/mobile-backend-starter-api-reference/#ciagaa)美国


  

在下面的code,后端通过允许客户端固定在通话
  标识模式。它还设置createdBy / updatedBy /所有者性质
  自动CloudEntity


  GoogleAccountCredential凭证=
  GoogleAccountCredential.usingAudience(这一点,< Web客户端ID>中);
credential.setSelectedAccountName(<谷歌帐户名称>);
cloudBackend.setCredential(凭证);

所以我写了下面code

mCloudBackend =新CloudBackendMessaging(本);

  GoogleAccountCredential凭证= GoogleAccountCredential.usingAudience(这一点,Consts.AUTH_AUDIENCE);
    mCloudBackend.setCredential(凭证);
    字符串帐户名=
            mCloudBackend.getShared preferences()的getString(
                    Consts preF_KEY_ACCOUNT_NAME,NULL);
    credential.setSelectedAccountName(帐户名);
    mCloudBackend.setCredential(凭证);
    newPost.setId(updateRide);
    mCloudBackend.update(newPost,处理程序);

很不幸,这是给上述错误。但是,更新正在经历,因为我可以看到实体的变化,当我查询的数据存储。这个问题似乎来自该createdBy / updatedBy /所有者属性被设置为null,因此也不会被自动设定的事实。

我看到那里的答案之前已经查询实体更新,使用它来设置上述属性,然后执行更新的其他问题。我宁愿避免这种情况,因为它似乎是到数据存储不必要的电话。所以我的问题是,如何我得到的Google账户createdBy updatedBy和所有者属性?


虽然我遵循了本<一提供的答案
解决方案

我遇到类似的问题,而与移动后端首发打source.And甚至href=\"http://stackoverflow.com/questions/23231800/google-cloud-datastore-mobile-backend-starter-permissions-failure-on-update\">link,I是不是能够解决issue.However,我所做的就是抢手机后端源$ C ​​$ C,并提出一些modifications.Grab的code和在 CrudOperations.java 的文件,你会看到这个方法

 私人地图&LT;弦乐,实体&GT; findAndUpdateExistingEntities(EntityListDto CDL,用户用户)
 抛出UnauthorizedException {//创建与现有的CE标识的列表
EntityListDto entitiesWithIds =新EntityListDto();
地图&LT;弦乐,EntityDto&GT; entitiesWithIdMap =新的HashMap&LT;弦乐,EntityDto&GT;();
为(EntityDto CD:cdl.getEntries()){
  如果(cd.getId()!= NULL){
    entitiesWithIds.add(CD);
    entitiesWithIdMap.put(cd.getId(),CD);
  }
}//试图让现有的CE
地图&LT;弦乐,实体&GT; existingEntities = getAllEntitiesByKeyList(entitiesWithIds
    .readKeyList(用户));//更新现有实体
对于(字符串ID:existingEntities.keySet()){  //检查ACL
  实体E = existingEntities.get(ID);
  。SecurityChecker.getInstance()checkAclForWrite(即用户);  //更新元数据
  EntityDto CD1 = entitiesWithIdMap.get(ID);
  cd1.setUpdatedAt(新的Date());
  如果(用户!= NULL){
    cd1.setUpdatedBy(user.getEmail());
  }  //更新实体
  cd1.copyPropValues​​ToEntity(E);
}
返回existingEntities;

}

从上面的code,注释掉 SecurityChecker.getInstance()checkAclForWrite(即用户)。
抛出UnAuthorizedException 线和重新部署backend.Doing这样会使你的应用程序的所有用户能够进行更新,有关entity.This可能是有风险的,如果你严格关注ownership.So,采取这一approach.Haven做,你现在可以自由地更新有关云entity.Remember使默认服务器端的最新部署的后端。前先考虑您的安全问题

I am using mobile backend starter and I am trying to update an entity when using the secured by id setting. I keep getting the error

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
    {
    "code": 401,
    "errors": [
    {
    "domain": "global",
    "location": "Authorization",
    "locationType": "header",
    "message": "Insuffient permission for updating a CloudEntity: CE:123456 by: USER:123456",
    "reason": "required"
    }
    ],
    "message": "Insuffient permission for updating a CloudEntity: CE: 123456 by: USER:123456"
    }

The documentation (https://cloud.google.com/developers/articles/mobile-backend-starter-api-reference/#ciagaa) states

In the code below, the backend allows the call in "Secured by Client ID" mode. It also sets createdBy/updatedBy/owner properties of CloudEntity automatically

GoogleAccountCredential credential =
  GoogleAccountCredential.usingAudience(this, "<Web Client ID>");
credential.setSelectedAccountName("<Google Account Name>");
cloudBackend.setCredential(credential);

So I wrote the following code

mCloudBackend = new CloudBackendMessaging(this);

GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(this, Consts.AUTH_AUDIENCE);
    mCloudBackend.setCredential(credential);
    String accountName =
            mCloudBackend.getSharedPreferences().getString(
                    Consts.PREF_KEY_ACCOUNT_NAME, null);
    credential.setSelectedAccountName(accountName);
    mCloudBackend.setCredential(credential);
    newPost.setId(updateRide);
    mCloudBackend.update(newPost, handler);

Unfortunately this is giving the error above. However, the update is going through as I can see changes in entity when I query the datastore. The problem seems to come from the fact that the createdBy/updatedBy/owner properties are set to null and so are not being set automatically.

I have seen other questions where the answer has been to query the entity prior to the update, use this to set the aforementioned properties and then perform the update. I would rather avoid this as it seems like an unnecessary call to the datastore. So my question is how to I get the GoogleAccount createdBy updatedBy and owner properties?

解决方案

I faced similar problem while playing with the mobile backend starter source.And even though I followed the answers provided in this link,I wasn't able to resolve the issue.However,what I did was to grab the mobile backend source code and make some modifications.Grab the code and in the CrudOperations.java file,you will see this method

private Map<String, Entity> findAndUpdateExistingEntities(EntityListDto cdl, User user)
 throws UnauthorizedException{

// create a list of CEs with existing Id
EntityListDto entitiesWithIds = new EntityListDto();
Map<String, EntityDto> entitiesWithIdMap = new HashMap<String, EntityDto>();
for (EntityDto cd : cdl.getEntries()) {
  if (cd.getId() != null) {
    entitiesWithIds.add(cd);
    entitiesWithIdMap.put(cd.getId(), cd);
  }
}

// try to get existing CEs
Map<String, Entity> existingEntities = getAllEntitiesByKeyList(entitiesWithIds
    .readKeyList(user));

// update existing entities
for (String id : existingEntities.keySet()) {

  // check ACL
  Entity e = existingEntities.get(id);
  SecurityChecker.getInstance().checkAclForWrite(e, user);

  // update metadata
  EntityDto cd1 = entitiesWithIdMap.get(id);
  cd1.setUpdatedAt(new Date());
  if (user != null) {
    cd1.setUpdatedBy(user.getEmail());
  }

  // update the entity
  cd1.copyPropValuesToEntity(e);
}
return existingEntities;

}

From the above code,comment out SecurityChecker.getInstance().checkAclForWrite(e, user); and the throws UnAuthorizedException lines and redeploy your backend.Doing so will make all users of your app able to make updates to the concerned entity.This could be risky if you are strictly concerned about ownership.So,consider your security concerns before taking this approach.Haven done that,you can now freely update the concerned cloud entity.Remember to make as default your newly deployed backend on the server side.

这篇关于cloudBackend.setCredential没有设置CloudEntity的createdBy / updatedBy /所有者性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆