【问题标题】:Angular: API call - 401 not authorizedAngular:API 调用 - 401 未授权
【发布时间】:2019-02-25 02:30:30
【问题描述】:

我正在尝试访问 API 并碰壁...反复。我已经阅读了 Angular 文档,但我仍然遗漏了一些东西。请指教。谢谢!

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, catchError, tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class RestService {
  private url = 'https://api.com';
  private auth = ('xxxx:xxxx');

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Content-Encoding': 'none',
      'Authorization': 'Basic' + btoa(this.auth)

    })
  };

  constructor(private http: HttpClient) {
  }

  index() {
    return this.http.get<any[]>(this.url, this.httpOptions);
  }

}

【问题讨论】:

  • 我相信问题出在授权上,在 Basic 后面加一个空格
  • 谢谢。我正在阅读它并看到相似之处,但我使用的是较新版本的 Angular,我并没有完全看到我的错误。
  • @Ricardo - 就是这样。我已经为此工作了4天。我告诉我的妻子,这可能是某个地方的空格或逗号。如果那不是真的,该死。谢谢你。你也一样,vuluu

标签: angular api


【解决方案1】:

在您的授权标头上,在单词 Basic 和您的身份验证密钥之间创建一个空格。像这样:

'Authorization': 'Basic ' + btoa(this.auth)

【讨论】:

  • 谢谢!我很感激;这就是解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 2018-05-19
  • 2020-09-29
  • 2020-06-11
  • 2017-02-22
相关资源
最近更新 更多