Authentication issue while accessing Sonarqube api from Angular

200 Views Asked by At

I need to access Sonarqube Api from Angular 8 app but my request call is failing, I have tried following way:

app.service.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 was generated in 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);
  }
}

but I'm not able to authenticate with this way, any idea what's going wrong here?

Thanks in advance.

1

There are 1 best solutions below

6
RICHARD ABRAHAM On

Can you try the below if it helps:

@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);
  }
}

ensure you are subscribing to the HTTP request to the getIssues() method of the AppService