使用“new"获取 403 响应.Firebase 云消息传递 API

Get 403 response with the quot;newquot; Firebase Cloud Messaging API(使用“new获取 403 响应.Firebase 云消息传递 API)
本文介绍了使用“new"获取 403 响应.Firebase 云消息传递 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已成功使用 旧版 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)