【发布时间】:2017-12-24 22:50:24
【问题描述】:
我在基于 Spring 的应用服务器中使用 Firebase Cloud Messaging REST API 向我的应用客户端发送推送消息。
从我的 IDE 运行时,一切正常。 问题是从打包的 JAR 文件运行并尝试发送推送消息时,我得到:“身份验证凭据无效”和状态码 401。
我的 service-account.json 文件位于资源文件夹下,该文件夹已添加到类路径中:
我通过以下方式访问它:
private String getAccessToken() throws IOException {
Resource resource = new ClassPathResource("service-account.json");
GoogleCredential googleCredential = GoogleCredential
.fromStream(resource.getInputStream())
.createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
我也尝试过访问 service-account.json 的不同方法,例如将其放在项目根目录中并像这样检索它:
private String getAccessToken() throws IOException {
File file = new File("service-account.json");
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream(file))
.createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
当从打包的 JAR 运行时,我在 JAR 之外提供了 service-account.json 文件,与 JAR 位于同一文件夹中。 这导致了同样的错误。
我真的不确定为什么会发生这种情况,感谢任何帮助或猜测。
【问题讨论】:
标签: java spring push-notification firebase-cloud-messaging