0

我是 Office 365 的新手,在访问 rest api 时遇到问题。我正在尝试测试Calendar 和 Mail API的其余 api ,所以我决定使用 Postman。但是,为了测试这些 API,我需要 Authorization 标头中的访问令牌。为了弄清楚如何获取令牌,我决定在这里获取示例项目,在此本地站点上配置、运行和登录,以获取缓存在本地存储中的令牌,并将该令牌用于 Postman 中的进一步请求。但是,我测试的所有请求都返回了“401 未经授权的请求”。

我做了什么:

  • 在与 O365 帐户关联的 Azure ADD 上注册新应用
  • 添加完整的应用权限和委派权限。
  • 在清单文件中将“oauth2AllowImplicitFlow”更新为 true。
  • 克隆示例项目
  • 在 app.js 中,我将更改配置功能的内容如下

    function config($routeProvider, $httpProvider, adalAuthenticationServiceProvider) {
        $routeProvider
        .when('/', {
            templateUrl: 'views/home.html',
            controller: 'HomeController',
            controllerAs: 'home',
            requireADLogin: true
        })
    
        .otherwise({
            redirectTo: '/'
        });
    
    // The endpoints here are resources for ADAL to get tokens for.
    var endpoints = {
        'https://outlook.office365.com': 'https://outlook.office365.com'
    };
    
    // Initialize the ADAL provider with your tenant name and clientID (found in the Azure Management Portal).
    adalAuthenticationServiceProvider.init(
        {
            tenant: 'mytenantname.onmicrosoft.com',
            clientId: '<my cliend Id>',
            endpoints: endpoints,
            cacheLocation: 'localStorage'
        },
        $httpProvider
        );
    };
    

然后我运行了应用程序,它让我登录很好,我也可以获得令牌,但该令牌也未经授权请求。我解码了令牌并看到了“aud”的值,它没有返回“ https://outlook.office365.com/ ”。在这个 url中,作者说“这应该是” https://outlook.office365.com/ “用于邮件、日历或联系人 API”

那么我错过了什么?

4

1 回答 1

1

如何在 AngularJS 中调用 Office 365 API?

登录用户时,您只会获得 id_token 来验证用户。

id_token 的 aud 是租户 id (GUID)。

要调用 Office 365 API,需要使用 AugularJS http 请求。

以下是在 AngularJS 中使用 Microsoft Graph API 发送电子邮件的示例:

  // Build the HTTP request to send an email.
  var request = {
    method: 'POST',
    url: 'https://graph.microsoft.com/v1.0/me/microsoft.graph.sendmail',
    data: email
  };

  // Execute the HTTP request. 
  $http(request)
    .then(function (response) {
      $log.debug('HTTP request to Microsoft Graph API returned successfully.', response);     
      response.status === 202 ? vm.requestSuccess = true : vm.requestSuccess = false; 
      vm.requestFinished = true;
    }, function (error) {
      $log.error('HTTP request to Microsoft Graph API failed.');
      vm.requestSuccess= false;
      vm.requestFinished = true;
    });

在调用 API 之前,ADAL.js 将获取另一个令牌 - 访问令牌,您可以使用它来发送电子邮件。

在此处输入图像描述

更新#1

我还下载了您提到的示例。要运行此示例,请确保您在应用程序中分配了Exchange Online > 读取和写入用户邮件权限。

在此处输入图像描述

于 2016-05-10T13:23:22.893 回答