在Get方法Angular 9上获得401未经授权未提供身份验证凭据

问题描述

我遇到授权问题。当我尝试从服务器获取数据时,我得到了

error: {detail: "Authentication credentials were not provided."}
headers: HttpHeaders {normalizednames: Map(0),lazyUpdate: null,lazyInit: ƒ}
message: "Http failure response for http://someDomein.com/api/v1/users/: 401 
Unauthorized"
name: "HttpErrorResponse"
ok: false
status: 401
statusText: "Unauthorized"

我的服务是

export class UserService {

  public userList: User[] = [];
  headers: Headers = new Headers();
  constructor(private http: HttpClient) {}


  fetchUsers(): Observable <User[]> {

      const headers = new HttpHeaders({
    'Content-Type': 'application/json',});
      this.headers.append('Authorization','Bearer ' + localStorage.getItem('auth-token'));
      const options = {
    headers
  };
      return this.http.get<User[]>('http://someDomain.com/api/v1/users/',options)
        .pipe( tap(userList => this.userList = userList));
    }

  }

我的中间人是

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor(private auth: AuthService) {}

  intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
    const token = this.auth.getToken();

    req = req.clone({
      setHeaders: {
          'Content-Type': 'application/json',Authorization: `${token}`
      },});

    return next.handle(req);
  }
}

我在此网站上检查了很多答案,但没有找到答案。 预先感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)