如何使用Angular 5中的身份验证标头连接到第三方API

时间:2018-04-12 01:47:59

标签: angular api base64 authorization angular-http-interceptors

我无法使用Base64编码格式username:password格式的身份验证标头连接第三方API

以下是我的Angular服务代码:auth.service.ts

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from 
'@angular/common/http';
import {Observable} from "rxjs/Observable";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(){}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> 
{
    const authReq = req.clone({
        setHeaders: {
          Authorization: `Basic bWluaXByb2plY3Q6UHIhbnQxMjM=`
        }
      });
return next.handle(req);
}
}

这是app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AppComponent } from './app.component';
import { AuthInterceptor } from './interceptors/auth.service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
  ],
  providers: [{
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true
    }],
  bootstrap: [AppComponent]
})
export class AppModule { }

当我尝试通过GET呼叫连接

时出现错误
The SSL certificate used to load resources from https://api... will 
be distrusted in M70. Once distrusted, users will be prevented from loading 
these resources. See https://g.co/chrome/symantecpkicerts for more 
information.

任何方向都非常感谢。谢谢!

0 个答案:

没有答案