首页 > 解决方案 > 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 之后尝试了空白)我认为身份验证过程将由服务器密钥完成。

这只是一个测试环境设置,足以看到身份验证过程正常工作并发送消息。

标签: springfirebaseauthenticationfirebase-cloud-messaging

解决方案


推荐阅读