HTTPCLIENT Angular Post因Google FCM服务器而失败,如何修复?
我能够使用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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTP响应状态代码401是指未经授权,因此您的请求中似乎不存在您的携带者令牌标头。
Http response status code 401 means unauthorized, so it seems that your bearer token header is not present in your request.