【问题标题】:Firebase Push - Authentication credentials invalidFirebase 推送 - 身份验证凭据无效
【发布时间】: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


【解决方案1】:

最终我通过从应用程序外部接收到 service-account.json 的完整路径来解决它:

@Value("${service.account.path}")
private String serviceAccountPath;

在 application.properties 中:

service.account.path = /path/to/service-account.json

还有代码:

private String getAccessToken() throws IOException {
    GoogleCredential googleCredential = GoogleCredential
            .fromStream(getServiceAccountInputStream())
            .createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
    googleCredential.refreshToken();
    return googleCredential.getAccessToken();
}

private InputStream getServiceAccountInputStream() {
    File file = new File(serviceAccountPath);
    try {
        return new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Couldn't find service-account.json");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2014-10-15
    • 2021-12-19
    • 2020-04-10
    • 1970-01-01
    • 2022-06-11
    • 2018-11-26
    相关资源
    最近更新 更多