spring - Spring Firebase 云消息连接身份验证失败
问题描述
我第一次使用 firebase,我对云消息传递的使用有点困惑。
环境:
我有一个 Spring 微服务,它应该通过它们的令牌向应用程序发送消息(数据而不是通知)。(平台无关)作为协议,我想使用推荐的 Http v1。
我创建了一个 firebase 帐户,添加了一个测试项目,现在我想将数据放在那里。据我所知,没有办法看到推送的消息进行调试?
我没有应用程序来测试流程,因为它将由外部开发。
当前行为:
I just want to create a first test message. Therefor I use the following code.
// myProject from settings- general->ProjectID
String FIREBASE_API_URL = "https://fcm.googleapis.com/v1/projects/myProject/messages:send";
// From settings->cloud-messaging
String FIREBASE_SERVER_KEY = "newServerKey";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Authorization", "Bearer" + FIREBASE_SERVER_KEY);
httpHeaders.set("Content-Type", "application/json");
JSONObject json = new JSONObject();
JSONObject message = new JSONObject();
message.put("title","Hallo");
message.put("body","TEst");
json.put("data", message);
//I was hoping that this has not to be a valid token for testing.
json.put("to", "receiverFcmKey");
System.out.println("Sending :" + json.toString());
HttpEntity<String> httpEntity = new HttpEntity<>(json.toString(), httpHeaders);
for(int i =0; i<1; i++) {
System.out.println("Count:"+ i);
System.out.println(restTemplate.postForObject(FIREBASE_API_URL, httpEntity, String.class));
}
在这里我得到错误:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]
在这里,我读到我可能在我的 google apis 设置中启用了身份验证。
问题: 我不知道我哪里出错了。我真的必须先在谷歌设置中启用api吗?标题中的 auth 设置是否错误?(我在 Bearer 之后尝试了空白)我认为身份验证过程将由服务器密钥完成。
这只是一个测试环境设置,足以看到身份验证过程正常工作并发送消息。
解决方案
推荐阅读
- windows - 为什么 time.Now().UnixNano() 在 IO 操作后返回相同的结果?
- networking - 以无线方式将连接的设备访问本地网络
- css - CSS中的下拉菜单无法正常工作
- ios - 如何在 Xcode 中重新启用或使用情节提要?
- python - 我正在尝试使用 grid_forget 删除我的小部件,但它不起作用
- android - 编译 C 文件以便在 android 中使用
- ios - 我可以拥有具有不同 BundleId 和相同显示名称的一个应用程序的两个版本吗?
- django - 我在 django 2.2 中遇到错误,即 NOT NULL 约束失败:home_profile.user_id
- angular - 我正在使用 angular6 并且在最初加载页面时未显示标签 ng-content 内的内容
- android - WhatsApp 自定义 url 方案在 Webview 中不起作用