2017-12-24 22 views
2

SpringベースのアプリケーションサーバーでFirebase Cloud Messaging REST APIを使用して、プッシュメッセージをアプリケーションクライアントに送信しています。Firebaseプッシュ認証資格が無効です

私のIDEから実行すると、すべてが完全に機能します。 パッケージ化されたJARファイルから実行してプッシュメッセージを送信しようとすると、「認証情報が無効です」とステータスコード401が表示されます。

私のservice-account.jsonファイルは、クラスパスへ:私はまた、placiなどのサービス-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

プロジェクトのルートでそれをngのと、このようにそれを取得する:

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外サービスaccount.jsonファイルを供給パッケージ化JARから実行されています。 同じエラーが発生しました。

なぜこれが起こっているのか分かりません。何か助けや憶測がありがたいです。 application.propertiesで

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

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

とコード:

+0

それをチェックしてください! https://groups.google.com/forum/#!topic/firebase-talk/i2sqxJIRzno –

答えて

1

は、最終的に、私はアプリケーション外部からサービス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"); 
    } 
} 
関連する問題