I am working on a webapp which is hosted on Azure, uses Microsoft Graph API to sign users in using the corporate account email. Now, I'd like to read the sign in user and display him on my view, however, I am getting an InvalidAuthenticationToken error.
AuthHelper.ts:
import { Injectable } from "@angular/core";
import { SvcConsts } from "../service/svcConsts";
import { HttpModule } from "@angular/http";
import { HttpClientModule } from "@angular/common/http";
@Injectable()
export class AuthHelper {
//function to parse the url query string
private parseQueryString = function(url) {
var params = {},
queryString = url.substring(1),
regex = /([^&=]+)=([^&]*)/g,
m;
while ((m = regex.exec(queryString))) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return params;
};
private params = this.parseQueryString(location.hash);
public access_token: string = null;
constructor() {
//check for id_token or access_token in url
if (this.params["id_token"] != null) this.getAccessToken();
else if (this.params["access_token"] != null) this.access_token = this.params["access_token"];
}
login() {
//redirect to get id_token
window.location.href =
"https://login.microsoftonline.com/" +
SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=id_token&client_id=" +
SvcConsts.CLIENT_ID +
"&redirect_uri=" +
encodeURIComponent(window.location.href) +
"&state=SomeState&nonce=SomeNonce";
}
private getAccessToken() {
//redirect to get access_token
window.location.href =
"https://login.microsoftonline.com/" +
SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=token&client_id=" +
SvcConsts.CLIENT_ID +
"&resource=" +
SvcConsts.GRAPH_RESOURCE +
"&redirect_uri=" +
encodeURIComponent(window.location.href) +
"&prompt=none&state=SomeState&nonce=SomeNonce";
}
}
SvsConsts.ts:
export class SvcConsts {
public static CLIENT_ID: string = "my client id here";
public static TENANT_ID: string = "mydomain.azurewebsites.net";
public static GRAPH_RESOURCE: string = "https://graph.microsoft.com";
}
HomeComponents.ts:
import { Component } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { AuthHelper } from "../service/AuthHelper";
import { HttpModule } from "@angular/http";
import { HttpClientModule } from "@angular/common/http";
@Component({
selector: "app-home",
templateUrl: "./home.component.html"
})
export class HomeComponent {
private users: any;
private userPrincipalName: any;
constructor(http: Http, authHelper: AuthHelper) {
// Perform REST call into Microsoft Graph for files on GraphAPI
http
.get("https://graph.microsoft.com/v1.0/me/", {
headers: new Headers({
Authorization: "Bearer { access_token }",
"Content-Type": "application/json" + authHelper.access_token
})
})
.subscribe(res => {
// Check the response status before trying to display files
if (res.status === 200) this.users = res.json().value;
else alert("An error occurred calling the Microsoft Graph: " + res.status);
});
}
}
I am planning to use the userPrincipalName property in Home.Component.html to display, however, I am getting that error. I followed the example provided by Graph GitHub repository and followed documentation on their website. Can you help me with why am I getting this error?
Below is the screen capture of the permissions I have on Azure AD management tool.
Please help.
