【问题标题】:Use ng2-adal to get access token for Microsoft Graph Client使用 ng2-adal 获取 Microsoft Graph 客户端的访问令牌
【发布时间】:2017-07-04 12:01:07
【问题描述】:

我正在尝试创建一个 Angular 应用程序,该应用程序使用 Angular 2 ADAL library 登录 Azure Active Directory,然后调用 Microsoft Graph Client 以检索有关当前用户的一些信息。

不幸的是,Graph 客户端总是返回InvalidAuthenticationToken,我不知道如何进一步调查以找到根本原因。

my.component.ts

import { Component, Inject, OnInit } from '@angular/core';
import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import { Client } from '@microsoft/microsoft-graph-client';

import { SecretService } from '../../shared/secret.service';
import { AdalService } from 'ng2-adal/services/adal.service';

@Component({
    selector: 'my',
    templateUrl: './my.component.html'
})
export class MyComponent implements OnInit {
    isBrowser: boolean;
    private graphClient: Client;
    private userProfile: any;

    constructor(
        @Inject(PLATFORM_ID) platformId: Object,
        private adalService: AdalService,
        private secretService: SecretService) {
        this.isBrowser = isPlatformBrowser(platformId);
        adalService.init(secretService.adalConfig);

        // Don't initialize graph client in server-side-rendering
        if (this.isBrowser) {
            this.graphClient = Client.init({
                authProvider: (done) => {
                    done(undefined, this.adalService.getCachedToken(this.secretService.adalConfig.clientId));
                }
            });
        }
    }

    get isLoggedIn(): boolean {
        if (!this.isBrowser)
            return false;

        return this.adalService.userInfo.isAuthenticated;
    }

    ngOnInit() {
        // Fast exit on server-side-rendering
        if (!this.isBrowser)
            return;

        // Initialize ADAL service
        this.adalService.handleWindowCallback();
        this.adalService.getUser();

        // If we are already logged in (cause reply url is called from login)
        // use Graph API to get some data about the current user
        if (this.isLoggedIn) {
            this.graphClient.api('/me').get().then((value) => {
                this.userProfile = value;
            }).catch((error) => {
                // Currently I'm always getting here, but never in the above then() call.
                console.log(error);
            });
        }
    }

    onLogin() {
        this.adalService.login();
    }

    onLogout() {
        this.adalService.logOut();
    }
}

my.component.html

<md-toolbar>
    <md-toolbar-row>
        <button color="primary" md-button *ngIf="!isLoggedIn" (click)="onLogin()">
            Login
        </button>
        <button color="accent" md-button *ngIf="isLoggedIn" (click)="onLogout()">
            Logout
        </button>
    </md-toolbar-row>
</md-toolbar>
<md-card>
    <md-card-content>
        <section>
            {{userProfile}}
        </section>
    </md-card-content>
</md-card>

【问题讨论】:

    标签: angular microsoft-graph-api adal.js


    【解决方案1】:

    根据代码,您使用 Azure AD 发出的 id_token 调用 Microsoft Graph。要调用 Microsoft Graph,我们需要使用 access_token,它的受众应该是https://graph.microsoft.com

    您需要使用如下代码获取 Microsoft Graph 的 access_token:

    this.adalService.acquireToken("https://graph.microsoft.com").subscribe(function(token){
              this.graphClient = Client.init({
                authProvider: (done) => {
                    done(undefined, token);
                }                
            });
    

    关于Microsoft Graph认证的更多细节,您可以参考以下链接:

    Get access tokens to call Microsoft Graph

    【讨论】:

    • 好的,现在我明白了acquireToken() 调用中的resource 应该是什么。我相应地更改了我的代码并重试了它。现在acquireToken() 调用返回错误:Token renewal operation failed due to timeout。通过查看网络记录器,我可以看到对https://login.microsoftonline.com/myDomain.onmicrosoft.com/oauth2/authorize?response_type=token&amp;... 的调用,但该调用的状态为(canceled)。有什么想法吗?
    • Adal 等待 6 秒以接收来自 AAD 的响应。如果 AAD 根本不发送响应或响应没有作为片段发送,adal 会取消请求并给出上述错误。您能否检查网络选项卡(在 Chrome 的开发人员工具中)或提琴手,看看 AAD 是否返回了对 adal 请求的响应。 adal 请求被发送到 login.microsoftonline.com 并以 authorize? 开头?
    • 这就是我所看到的(并且已经在我的第一条评论中说明了)。唯一尴尬的是时间。它的值总是在 220 毫秒到 400 毫秒之间。我还尝试了在我的 azure 门户中声明的图形 api 端点(https://graph.windows.net/tenant-GUID)。这是我的网络标签中的screenshot
    • 由于我无法重现此问题,您介意分享一个可运行的代码示例来帮助重现此问题吗?您可以将其上传到 Github,并在公开代码示例之前删除敏感信息。
    • 好的。我将发布一个示例项目。当它启动并运行时,我会通知你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多