Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

400 response from GAE Cloud Endpoints

I'm trying the following api call to my GAE Cloud Endpoint:

gapi.client.myapp.foo.update({
  "value": "foobar",
  "key": "keyvaluefromlistoperation"
}).execute(function(resp) {
  console.log(resp);
});

Which responds with the following:

[
 {
  "error": {
   "code": 400,
   "message": "Bad Request",
   "data": [
    {
     "domain": "usageLimits",
     "reason": "keyInvalid",
     "message": "Bad Request"
    }
   ]
  },
  "id": "gapiRpc"
 }
]

Note, prior to this call I have authenticated, inserted several foo objects, then call list to have them returned to the client. The api's explorer update call works fine and running the jQuery snippet below works fine as well. Any suggestions? Or am I just in experimental bug land.

var token = gapi.auth.getToken();
$.ajax({
  type:"POST",
  beforeSend: function (request) {
    request.setRequestHeader("Content-Type","application/json");
    request.setRequestHeader("Authorization", token.token_type+" "+token.access_token);
  },
  url: "https://myappid.appspot.com/_ah/api/myapp/v1/foo/update",
  data:JSON.stringify({
     "value": "foobar",
     "key": "keyvaluefromlistoperation"
  }),
  processData: false,
  dataType: "json",
  success: function(msg) {
    console.log(msg);
  },
  failure: function(msg) {
     console.log(msg);
  }
});

Here is the Java code:

@Api(
    name = "myapp",
    description = "This is the myapp rest interface",
    scopes = {"https://www.googleapis.com/auth/userinfo.email"},
    version = "v1",
    clientIds = {Ids.WEB_CLIENT_ID}
)
public class FooV1 {

    private static PersistenceManager getPersistenceManager() {
        return PMF.get().getPersistenceManager();
    }

    @ApiMethod(
            name = "foo.update", 
            httpMethod = HttpMethod.POST
    )
    public Foo update(Foo foo, User user) throws OAuthRequestException, IOException, UnauthorizedUpdateException {
        PersistenceManager pm = PMF.get().getPersistenceManager();

        if (user != null) {
            try {
                Foo f = pm.getObjectById(Foo.class, foo.getId());
                if ( Security.isUpdateAuthorized(f, user) ) {
                    if( foo.getValue() != null ) f.setValue(foo.getValue());
                } else {
                    throw new UnauthorizedUpdateException("");
                }
            } finally {
                pm.close();
            }
        } else {
            throw new OAuthRequestException("Invalid user.");
        }

        return foo;
    }
}
like image 968
jrmerz Avatar asked Feb 24 '13 18:02

jrmerz


1 Answers

I had the same problem. Apparently you can't use "key" as a field once you deploy to GAE. Locally it worked fine.

like image 127
Christiaan Avatar answered Sep 21 '22 22:09

Christiaan