Start a new topic

InSufficient Credentials when i try to update a record

Hello,

I have a model called Order and i have a list of orders i want when the user click on item of it a dialogue appears and when he press create i will update the requests parameter of the order he just clicked 

i have the following error

  dataInsufficientCredentials

 The credentials used to authenticate this request are not authorized to run this operation. Please retry your request with appropriate credentials


this is my code 

menu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Order order = my_orders[position];
order.getAcl().setGloballyWriteable(true);
order.getAcl().setGloballyReadable(true);
int requests = order.getRequests();
////////////have the handler
AlertDialog.Builder alert = new AlertDialog.Builder(
new ContextThemeWrapper(ImageTargets.this, R.style.AlertDialogCustom));

LinearLayout layout = new LinearLayout(ImageTargets.this);
layout.setOrientation(LinearLayout.VERTICAL);
alert.setTitle("There are " + requests + "Before you");
alert.setView(layout);
alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
///////////////
order.getAcl().setGloballyWriteable(true);
order.getAcl().setGloballyReadable(true);
order.setRequests(order.getRequests() + 1);
AsyncAppData<Order> myevents = mKinveyClient.appData("Order", Order.class);
myevents.save(order, new KinveyClientCallback<Order>() {
@Override
public void onFailure(Throwable e) {
Log.i("TAG", "failed to save event data" + e.getMessage());
Log.i("TAG", sharedpreferences.getString("owner_name", ""));
}

@Override
public void onSuccess(Order r) {
Log.d("TAG", "saved data for entity " + r.getName());
Toast.makeText(getApplicationContext(), "Your Order was Created Sucessfully", Toast.LENGTH_SHORT).show();
}
});

}
////////////


});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});

alert.show();
////////////////

}
});



In shared mode, only the creator can edit a record and setGloballyWriteable is an edit action. If the creator and user requesting the order, are different, setGloballyWriteable won't execute successfully. This is what happened with you for setGloballyWriteable, setGloballyReadable and setRequests.


But, in shared mode, if a user creates an order and requests the order himself, setGloballyWriteable will execute successfully.


Regards,

Wani


1 person likes this

thank u very much for your answer but can not i set Globally read  or write for only one record in my collection ? why did not setGloballyWriteable(true); work?

Considering that you have a user properly logged in, this seems to be related to collection permissions.

Your collection is in shared mode, that's why you are able to read the order object created by somebody else. But, because only creator can modify the object, you are getting "insufficient credentials" error.

You have two options here. The first option is to change collection permissions to public so that anybody can edit any order object. You can do that in Kinvey web console by going to settings tab in the data browser for the Order collection. After this, you won't need to use setGloballyWriteable/setGloballyReadable.

Other option (better suitable according to me) is to create another collection for requests for specific orders. In this new collection, you will be able to store a reference of the order object as well as store user information about the user who requested it.

You can find necessary documentation related to collection permissions here - http://devcenter.kinvey.com/android/guides/security


Regards,

Wani

Kinvey Support


1 person likes this
Login or Signup to post a comment