首页 > 解决方案 > 在发布请求时获得 403

问题描述

角 11

Stackblitz:https ://stackblitz.com/edit/github-vunq3e

我试图发出一个帖子请求以标记喜欢的曲目:

服务.ts

MarklikedSongs(id:any, status:boolean){
    const token = "1072694e";
    let headers = new HttpHeaders({
      "Content-Type": "application/json",
      
      "user-access-token": token
    });
    const httpOptions = {
      headers: headers
    };
    return this.http.post(`https://api.sprintt.co/spotify/liked_tracks/${id}?status=${status}`, httpOptions);
 
  }

组件.ts

markLikedSongs(id:any){
    this.playListsAPI.MarklikedSongs(id,true).subscribe((data:any)=>{
      this.likedSongs = data
      console.log(" this.likedSongs:",  this.likedSongs)
    })
  }

由于未知原因,我在尝试从客户端发出请求时收到 403 错误消息: “无法识别令牌”,或者更准确地说,令牌甚至没有发送到标头。尽管在 Postman 和我的其他 motheds 中一切都检查出来了..

在此处输入图像描述

在此处输入图像描述 谢谢!

标签: angularpost

解决方案


令牌在 Authorization 标头中传递。

试试这个:用 auth_token 替换你的令牌。

 const headers = new Headers({
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${auth_token}`
  });

推荐阅读