HTTPCLIENT Angular Post因Google FCM服务器而失败,如何修复?

发布于 2025-02-13 15:27:16 字数 1122 浏览 0 评论 0原文

我能够使用Postman成功地通过帖子和HTTP V1 Firebase API发送FCM消息到我的应用程序,但是当我在Angular httprequest中使用同一帖子时,它会失败:

import { Injectable } from '@angular/core';
import { HttpClient , HttpResponse, HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from "rxjs/operators";

sendFCMMessage(accessToken : string, data: object) {
console.log('sendFCMMessage');
console.log(accessToken);
console.log(data);

const authString : string = 'Bearer '+ accessToken;
console.log(authString);
const url = 'https://fcm.googleapis.com/v1/projects/***-notifications/messages:send';
const httpOptions = { headers: new HttpHeaders({
  'Content-Type':  'application/json',
  'Authorization': authString
})};

const body =
    {
    "message":{
  "token":"wwrwjeriwe8***",
  "notification":{
    "body":"The Knicks are winning!",
    "title":"Sports App"
    }
   }
};

return this.http.post(url,body,httpOptions).pipe(map(res => res));
}

​然后订阅响应并获得上面粘贴的错误。有人知道为什么我会遇到此错误以及如何解决吗?我能够使用相同的确切发布URL,车身和标头完美地在Postman中完美发送FCM消息。

谢谢

I am able to successfully send FCM messages via a POST and HTTP v1 Firebase API to my app using Postman but when I use the same POST in Angular HttpRequest it fails:

Error

Here are my imports:

import { Injectable } from '@angular/core';
import { HttpClient , HttpResponse, HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from "rxjs/operators";

Here is my code in the service:

sendFCMMessage(accessToken : string, data: object) {
console.log('sendFCMMessage');
console.log(accessToken);
console.log(data);

const authString : string = 'Bearer '+ accessToken;
console.log(authString);
const url = 'https://fcm.googleapis.com/v1/projects/***-notifications/messages:send';
const httpOptions = { headers: new HttpHeaders({
  'Content-Type':  'application/json',
  'Authorization': authString
})};

const body =
    {
    "message":{
  "token":"wwrwjeriwe8***",
  "notification":{
    "body":"The Knicks are winning!",
    "title":"Sports App"
    }
   }
};

return this.http.post(url,body,httpOptions).pipe(map(res => res));
}

I then subscribe to the response and get the error pasted above. Does anyone know why I am getting this error and how to fix? I am able to send FCM messages perfectly in Postman using the same exact POST url, body and header.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2025-02-20 15:27:17

HTTP响应状态代码401是指未经授权,因此您的请求中似乎不存在您的携带者令牌标头。

    return this.http.post<any>(
      url,
      {
        message: {
          token: 'wwrwjeriwe8***',
          notification: {
            body: 'The Knicks are winning!',
            title: 'Sports App',
          },
        },
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': authString,
        },
      }
    );

Http response status code 401 means unauthorized, so it seems that your bearer token header is not present in your request.

    return this.http.post<any>(
      url,
      {
        message: {
          token: 'wwrwjeriwe8***',
          notification: {
            body: 'The Knicks are winning!',
            title: 'Sports App',
          },
        },
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': authString,
        },
      }
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文