CloudBackend.setCredential not setting createdBy / updatedBy / CloudEntity owner properties

I am using a mobile backend starter and I am trying to update an object using the secure by id parameter. I keep getting an 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 you to call the "Client-provided" ID "and also sets the createdBy / updatedBy / owner CloudEntity properties 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);

, . , , . , , , createdBy/updatedBy/owner null .

, , , , . , . , , GoogleAccount createdBy updatedBy ?

+2
1

. , , issue.However, , , . CrudOperations.java,

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;

}

SecurityChecker.getInstance().checkAclForWrite(e, user); throws UnAuthorizedException backend.Doing, . , . , . , . , .

+1

Source: https://habr.com/ru/post/1537594/


All Articles