使用"新"Firebase云消息传递API获取403响应

3dm*_*dmg 13 android firebase google-oauth firebase-cloud-messaging

我们在服务器上成功使用旧版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 ..."

我想,我想错了一小步,也许你可以帮忙吗?提前致谢!

编辑: 现在正在使用arterpa的暗示!再次感谢!

之后我得到400错误,因此请求数据中的某些内容是错误的:

问题是,我们没有将所有data{...}值转换为字符串.使用传统协议不是问题,但使用FCM HTTP v1 API时,它必须是字符串!;)

art*_*rpa 23

我遇到了这个问题,您似乎需要在Google API控制台上为您的项目启用FCM API .

  • 我说我之前遇到过这个问题,但修好了.我因为使用​​新的FCM HTTP v1 API而禁止使用403,因此您需要先在Google API信息中心启用FCM API,这在以前是旧版HTTP协议所不需要的. (3认同)
  • 谢谢!!FCM API已自动启用,但FCM Messaging API尚未启用.https://console.developers.google.com/apis/library/fcm.googleapis.com/ (3认同)