Parse: Unauthorized in REST API

I'm trying to send a push notification from my Java code consuming the Parse REST API with Jersey.

    Client client = Client.create();

    WebResource webResource = client.resource("https://api.parse.com/1/push");

    //My real id instead of XXXXXXXXXXXXXXXXXXXXXXXX    
    webResource.header("X-Parse-Application-Id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    //My real key instead of XXXXXXXXXXXXXXXXXXXXXXXX 
    webResource.header("X-Parse-REST-API-Key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    Map<String, Object> postData = new LinkedHashMap<>();
    postData.put("channels", Arrays.asList("")); //Broadcast notification

    Map<String, String> notificationData = new LinkedHashMap<>();
    notificationData.put("alert", "This is a notification");

    postData.put("data", notificationData);

    //Convert POST data to String
    String postDataAsStr = "";
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        postDataAsStr = objectMapper.writeValueAsString(postData);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    ClientResponse response = webResource
                    .accept(MediaType.APPLICATION_JSON)
                    .type(MediaType.APPLICATION_JSON)
                    .post(ClientResponse.class, postDataAsStr);

POST data is like:

{"channels":[""],"data":{"alert":"This is a notification"}}

But I am getting:

{"error":"unauthorized"}

Answers


Solved. I have added the headers this way:

 ClientResponse response = webResource
                .accept(MediaType.APPLICATION_JSON)
                .header("X-Parse-Application-Id", "XXXXXXXXXXXXXXXXXXXXXXXXXX")
                .header("X-Parse-REST-API-Key", "XXXXXXXXXXXXXXXXXXXXXXXXX")
                .type(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, postDataAsStr);

It looks like you need to add "Authorization" header as well.

webResource.header("Authorization", String.format("Negotiate %s", createTicket())

createTicket() implementation depends on your authorization method.


Need Your Help

How to detect the turnover motion in android?

android android-sensors

If the user turns over the phone I want to react to that by stopping my text to speech reading. It would be a nice feature for my app, but how can I detect this motion? I'm not really familiar with

Is it possible to determine if ruby code block is executed under http server or as standalone script?

ruby-on-rails ruby gem

I am writing a ruby gem and I want to be able to differentiate if gem is being used under http server (i.e invoked as a part of rails framework) or If ruby gem is is invoked from standalone script ...