首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ng2-adal获取的访问令牌

使用ng2-adal获取的访问令牌
EN

Stack Overflow用户
提问于 2017-07-04 12:01:07
回答 1查看 1.5K关注 0票数 0

我正在尝试创建一个使用角2 ADAL库登录到Azure Active的角度应用程序,然后调用Microsoft Graph来检索有关当前用户的一些信息。

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

my.component.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-05 06:26:06

基于代码,您正在使用Azure发布的id_token调用Microsoft。要调用MicrosoftGraph,我们需要使用access_token,它的受众应该是https://graph.microsoft.com

您需要使用如下代码为MicrosoftGraph获取access_token:

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

有关MicrosoftGraph的身份验证的更多细节,您可以参考下面的链接:

获取访问令牌以调用Microsoft

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44905480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档