-
Notifications
You must be signed in to change notification settings - Fork 7
/
graph.service.ts
55 lines (49 loc) · 1.78 KB
/
graph.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Injectable } from "@angular/core"
import { HttpClient, HttpErrorResponse, HttpHeaders } from "@angular/common/http"
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { MsalService } from "@azure/msal-angular";
@Injectable()
export class GraphService
{
private endpoint = "https://graph.microsoft.com/v1.0/";
constructor(private http: HttpClient, private authService: MsalService)
{
}
public getUserProfile(): Promise<Observable<string>>
{
var _this = this;
return this.authService.acquireTokenSilent(["User.Read"])
.then((token) =>
{
var options = {
headers: new HttpHeaders({'Authorization': 'Bearer ' + token})
};
return this.http.get(_this.endpoint + "me", options)
.map((response: Response) =>
{
return JSON.stringify(response);
})
.pipe(catchError(_this.handleError));
});
}
private handleError(error: HttpErrorResponse)
{
if (error.error instanceof ErrorEvent)
{
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
}
else
{
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
// return an observable with a user-facing error message
return throwError(
'Something bad happened; please try again later.');
};
}