0

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.

MM Mathur
  • 1
  • 1

1 Answers1

0

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

RICHARD ABRAHAM
  • 2,218
  • 20
  • 26
  • Not working even giving same error: To show the way I'm subscribing `onSubmit() { this.submitted = true; this.appService.getIssues(this.model.branch, this.model.project, this.model.severity) .subscribe((resp) => { this.issues = resp; }); }` – MM Mathur Jun 27 '20 at 14:45
  • Can you paste your app-routing file where you have configured the interceptor? – RICHARD ABRAHAM Jun 27 '20 at 14:48
  • I don't have routing but here is my app.module.ts `import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { AuthInterceptor } from './AuthInterceptor'; providers: [AppService, { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }],` – MM Mathur Jun 27 '20 at 14:58
  • Let me give more details about error I'm getting: ` Provisional headers are shown Accept: application/json, text/plain, */* Authorization: Basic AUTH_TOKEN Referer: http://localhost:4200/ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 ` Error: Status Code: 403 Forbidden – MM Mathur Jun 27 '20 at 17:19
  • Are you sure that the URL: https://sonarqube.[domain].com//api/issues/search?[queryparams] with the requires params work and yield some result? Can you try with postman or some rest client? – RICHARD ABRAHAM Jun 29 '20 at 05:03
  • queryparams are replaced with actually query params in my code and it works file with POSTMAN. – MM Mathur Jun 29 '20 at 13:36