我们已成功使用 旧版 HTTP 服务器协议我们的 FCM 服务器.我今天想更新到 FCM HTTP v1 API.
We are successfully using the Legacy HTTP Server Protocol on our server for FCM. I wanted to update to FCM HTTP v1 API today.
我做到了一步一步,当服务器调用请求,我们得到这个响应:
I did it step by step and when the server calls the request, we get this response:
Server returned HTTP response code: 403 for URL: https://fcm.googleapis.com/v1/projects/[projectid]/messages:send
这是服务器代码:
URL url = new URL("https://fcm.googleapis.com/v1/projects/[projectid]/messages:send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
conn.setRequestProperty("Content-Type", "application/json");
OutputStream outputStream = conn.getOutputStream();
outputStream.write(req.getBytes("UTF-8"));
// Exception happen here
InputStream inputStream = conn.getInputStream();
getAccessToken():
The getAccessToken():
private static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream(ClientApiServlet.context.getRealPath("/WEB-INF/[projectid].json"))) .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
我已经从 firebase 云的 adminsdk 页面下载了 json 文件.
I have downloaded the json file from the adminsdk page of the firebase cloud.
都具有相同的投影...
All with the same projectid...
我在服务器上更新了这两个库:
I updated these 2 libs on the server:
google-http-client-jackson2-1.23.0.jar
google-oauth-client-1.23.0.jar
getAccessToken() 方法返回一个访问令牌:ya29.c.Elr0BAa..."
The getAccessToken() methode returned an accesstoken: "ya29.c.Elr0BAa..."
我想,我错过了一小步,也许你能帮忙?提前致谢!
I think, I miss a small step, maybe you could help? Thanks in advance!
它现在在 arterpa 的提示下工作!再次感谢!
It is working now with the hint of arterpa! Thanks again!
之后我得到一个 400 错误,所以请求数据中有错误:
After that I got a 400 error, so something in the request data was wrong:
问题是,我们没有将所有 data{...}
值转换为字符串.对于遗留协议,这不是问题,但对于 FCM HTTP v1 API,它必须是字符串!;)
The problem was, we didn't converted all data{...}
values to strings. With the legacy protocol it was not an issue, but with FCM HTTP v1 API it has to be strings! ;)
我遇到了这个问题,您似乎需要在 Google API 控制台.
I had this problem, and it seems you need to enable FCM API for your project at Google API console.
这篇关于使用“new"获取 403 响应.Firebase 云消息传递 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!