我们成功地使用了 Legacy HTTP Server Protocol在我们的 FCM 服务器上。 我想更新到 FCM HTTP v1 API今天。
我做到了step by step当服务器调用请求时,我们得到这个响应:
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():
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文件。
都具有相同的 projectid...
我在服务器上更新了这两个库:
google-http-client-jackson2-1.23.0.jar
google-oauth-client-1.23.0.jar
getAccessToken() 方法返回了一个访问 token :“ya29.c.Elr0BAa...”
我想,我错过了一小步,也许你能帮忙? 提前致谢!
编辑: 它现在正在使用 arterpa 的提示!再次感谢!
在那之后我得到了一个 400 错误,所以请求数据中的某些东西是错误的:
问题是,我们没有将所有 data{...}
值都转换为字符串。对于遗留协议(protocol),这不是问题,但对于 FCM HTTP v1 API,它必须是字符串! ;)
最佳答案
我遇到了这个问题,看来您需要在 Google API console 为您的项目启用 FCM API .
关于android - 使用 "new"Firebase Cloud Messaging API 获取 403 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47019009/