gpt4 book ai didi

java - 解析: Unauthorized in REST API

转载 作者:行者123 更新时间:2023-12-01 11:35:10 25 4
gpt4 key购买 nike

我正在尝试从使用 Jersey 的 Parse REST API 的 Java 代码发送推送通知。

    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 数据如下:

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

但我得到:

{"error":"unauthorized"}

最佳答案

已解决。我以这种方式添加了标题:

 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);

关于java - 解析: Unauthorized in REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com