有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFirebase云消息突然返回401

我有一个非常令人困惑的问题。我正在设置一个管理应用程序,它将使用Firebase Cloud Messaging向目标客户端发送推送通知。它工作了一段时间,然后突然停止工作,我没有对代码做任何更改-这是令人困惑的部分

这是Firebase配置

FirebaseConfiguration。java

@Configuration
public class FirebaseConfig {

    private final String databaseUrl;

    private final String configPath;

    @Inject
    public FirebaseConfig(
            @Value("${firebase.database.url}") String databaseUrl, 
            @Value("${firebase.config.path}") String configPath) {
        this.databaseUrl = databaseUrl;
        this.configPath = configPath;
    }

    @Bean
    public DatabaseReference firebaseDatabase() {
        return FirebaseDatabase.getInstance().getReference();
    }

    @PostConstruct
    public void init() throws IOException {
        /*
         * https://firebase.google.com/docs/admin/setup Create service account; Download .json config file
         */
        final InputStream serviceAccount = FirebaseConfig.class.getClassLoader().getResourceAsStream(configPath);

        final FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl(databaseUrl)
                .build();

        FirebaseApp.initializeApp(options);
    }
}

这里,configPath.json文件的路径,该文件包含我的Firebase应用程序的所有配置,在我的应用程序的Firebase控制台中生成

myapp firebase配置。json

{
  "type": "service_account",
  "project_id": "myapp",
  "private_key_id": "8564e07po7cff976068c702dcdpodbbb0cu3e6",
  "private_key": "-----BEGIN PRIVATE KEY-----...\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-jo9yp@myapp.iam.gserviceaccount.com",
  "client_id": "12345",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-jo9yp%myapp.iam.gserviceaccount.com"
}

现在,试图发送推送通知的代码失败了

Message message = Message.builder()
                .putData("Message", "Test 123")
                .setToken(token)
                .build();

        ApiFuture<String> response = FirebaseMessaging.getInstance().sendAsync(message);
        final String result = response.get(); // throws the exception

正在抛出的异常的堆栈跟踪如下所示:

java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at com.google.firebase.tasks.Tasks.getResultOrThrowExecutionException(Tasks.java:179)
    at com.google.firebase.tasks.Tasks.await(Tasks.java:102)
    at com.google.firebase.internal.TaskToApiFuture.get(TaskToApiFuture.java:74)
    at com.newfaces.businesslogic.logic.MessageRequestLogicBean$1.run(MessageRequestLogicBean.java:75)
    at com.google.firebase.internal.TaskToApiFuture$1.onComplete(TaskToApiFuture.java:50)
    at com.google.firebase.tasks.OnCompleteCompletionListener$1.run(OnCompleteCompletionListener.java:54)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.firebase.tasks.OnCompleteCompletionListener.onComplete(OnCompleteCompletionListener.java:48)
    at com.google.firebase.tasks.TaskCompletionListenerQueue.flush(TaskCompletionListenerQueue.java:81)
    at com.google.firebase.tasks.TaskImpl.setException(TaskImpl.java:115)
    at com.google.firebase.tasks.Tasks$1.run(Tasks.java:84)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:213)
    at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
    at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:98)
    at com.google.firebase.messaging.FirebaseMessaging.handleSendHttpError(FirebaseMessaging.java:251)
    at com.google.firebase.messaging.FirebaseMessaging.makeSendRequest(FirebaseMessaging.java:238)

我在FirebaseMessaging类中找到引发此异常的根代码段:

  private void handleSendHttpError(HttpResponseException e) throws FirebaseMessagingException {
    MessagingServiceErrorResponse response = new MessagingServiceErrorResponse();
    try {
      JsonParser parser = jsonFactory.createJsonParser(e.getContent()); // HERE e.getContent() == null which throws the NullPointerException
      parser.parseAndClose(response);
    } catch (IOException ignored) {
      // ignored
    }

    String code = FCM_ERROR_CODES.get(response.getString("status"));
    if (code == null) {
      code = UNKNOWN_ERROR;
    }
    String msg = response.getString("message");
    if (Strings.isNullOrEmpty(msg)) {
      msg = String.format("Unexpected HTTP response with status: %d; body: %s",
          e.getStatusCode(), e.getContent());
    }
    throw new FirebaseMessagingException(code, msg, e);
  }

现在,考虑到这实际上并没有返回异常,调试是不可能的,但是我查看了我的Firebase API Metrics,发现服务器返回了错误代码401 - Unauthorized。为了验证这一点,我做了这样一个curl,如https://developers.google.com/cloud-messaging/http#checkAPIkey

# server_key=YOUR_SERVER_KEY

# curl --header "Authorization: key=$server_key" \
       --header Content-Type:"application/json" \
       https://gcm-http.googleapis.com/gcm/send \
       -d "{\"registration_ids\":[\"ABC\"]}"

我得到的回应是:

{"multicast_id":6706957967213497648,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1532214216265421%fa1b6a1df9fd7ecd"}]}

。。。指示请求已针对我从应用程序中使用的给定服务器密钥和注册令牌(注册id)进行了授权

这让我完全不知所措。因为同一个令牌在应用程序中不起作用,但它在curl启用时是完全授权的。我不知道如何进行此操作,非常感谢您的帮助

编辑

我还试着在dryRun模式下运行,这很有效


共 (0) 个答案