我们在服务器上成功使用旧版HTTP服务器协议进行FCM.我想今天更新到FCM HTTP v1 API.
我一步一步地做了,当服务器调用请求时,我们得到了这个响应:
Server returned HTTP response code: 403 for URL: https://fcm.googleapis.com/v1/projects/[projectid]/messages:send
Run Code Online (Sandbox Code Playgroud)
这是服务器代码:
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();
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我已经从firebase云的adminsdk页面下载了json文件.
所有人都有相同的项目......
我在服务器上更新了这两个库:
google-http-client-jackson2-1.23.0.jar
google-oauth-client-1.23.0.jar
Run Code Online (Sandbox Code Playgroud)
getAccessToken()方法返回了一个accessstoken:"ya29.c.Elr0BAa ..."
我想,我想错了一小步,也许你可以帮忙吗?提前致谢! …