0

我需要从 Angular 8 应用程序访问 Sonarqube Api,但我的请求调用失败,我尝试了以下方式:

应用服务.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class AppService {

constructor(private http: HttpClient) { }
private baseUrl:string = 'https://sonarqube.[domain].com';
private issueSearch:string = '/api/issues/search?'
getIssues() {
const url:string = 'https://sonarqube.[domain].com//api/issues/search?[queryparams]'
   return this.http.get(url);
  }
}

Auth token 是在 sonarqube-->My Account -->Security 中生成的

Authintercepter.ts

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

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(){

  }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
  setHeaders: {
    Authorization: `Basic AUTH_TOKEN` 
  }
});

  return next.handle(req);
  }
}

但我无法通过这种方式进行身份验证,知道这里出了什么问题吗?

提前致谢。

4

1 回答 1

0

如果有帮助,您可以尝试以下方法:

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

    return next.handle(modifiedRequest);
  }
}

确保您订阅了对 AppService 的 getIssues() 方法的 HTTP 请求

于 2020-06-26T18:45:28.297 回答