【问题标题】:Outlook REST API: V1IDToken is not applicable for the current protocolOutlook REST API:V1IDToken 不适用于当前协议
【发布时间】:2017-01-18 08:31:05
【问题描述】:

我正在尝试通过 Javascript 从 Outlook 帐户检索我的电子邮件。 我使用 ADAL 对自己进行身份验证,这似乎有效。但是,如果我尝试在对 Outlook REST API 的 GET 请求中使用返回的令牌,则会收到未经授权的错误,并在标头中包含以下附加信息: x-ms-diagnostics:2000001;reason="This token profile 'V1IdToken' is not applicable for the current protocol.";error_category="invalid_token"

我的请求如下:

function test2(token) { 
      try 
      { 
        var xhr = new XMLHttpRequest(); 
        xhr.open("GET", 'https://outlook.office.com/api/v1.0/me/messages'); 

        // The APIs require an OAuth access token in the Authorization header, formatted like this: 'Authorization: Bearer <token>'. 
        xhr.setRequestHeader("Authorization", "Bearer " + token); 

        // Process the response from the API.  
        xhr.onload = function () { 
          // ...
        } 

        // Make request.
        xhr.send(); 

我都尝试过 v1.0 和 v2.0 REST api。

【问题讨论】:

  • 如果答案有帮助,请告诉我。

标签: rest outlook adal


【解决方案1】:

根据您使用 id_token 请求 Outlook Online REST 的错误消息。 id_token 仅用于客户端对用户进行身份验证。要请求 Outlook 在线 REST,我们需要使用 访问令牌

如果您使用 angularJS 进行开发,则无需手动附加令牌。这是一个代码示例供您参考:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.13/js/adal.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.13/js/adal-angular.min.js"></script>
<script src="//unpkg.com/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>

<div ng-app="myApp">
    <div> Hello {{ userInfo.userName }}</div>
    <div ng-controller="homeCtrl">
        <ul class="nav navbar-nav navbar-right">
            <li><a ng-click="getMessages()">Get Messages</a></li>
            <li><a class="btn btn-link" ng-show="userInfo.isAuthenticated" ng-click="logout()">Logout</a></li>
            <li><a class="btn btn-link" ng-hide="userInfo.isAuthenticated" ng-click="login()">Login</a></li>
        </ul>

        <div>
            <table class="table table-striped">
                <tbody>
                    <tr data-ng-repeat="item in messages">
                        <td>
                            <p>{{item.Subject}}</p>
                        </td>
                        <td>
                            <p>{{item.Sender.EmailAddress.Address}}</p>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    var myApp = angular.module('myApp', ['AdalAngular'])
.config(['$httpProvider', 'adalAuthenticationServiceProvider', function ($httpProvider, adalProvider) {

    var endpoints = {
        "https://outlook.office.com": "https://outlook.office.com",
    };

    adalProvider.init(
       {
           instance: 'https://login.microsoftonline.com/',
           tenant: '{yourTenant}.onmicrosoft.com',
           clientId: '{yourAppId}',
           extraQueryParameter: 'nux=1',
           endpoints: endpoints,
       },
       $httpProvider
       );


}])
    myApp.controller('homeCtrl', ['$scope', '$http', 'adalAuthenticationService', '$location', 'toGetMessagesSvc', function ($scope, $http, adalService, $location, toGetMessagesSvc) {

        $scope.login = function () {
            adalService.login();
        };
        $scope.logout = function () {
            adalService.logOut();
        };

        $scope.getMessages = function () {
            toGetMessagesSvc.getMessages().success(function (results) {
                $scope.messages = results.value;
                $scope.loadingMessage = "";
            });
        }


    }]);

    myApp.factory('toGetMessagesSvc', ['$http', function ($http) {

        var apiEndpoint = "https://outlook.office.com/";
        $http.defaults.useXDomain = true;
        delete $http.defaults.headers.common['X-Requested-With'];
        return {
            getMessages: function () {
                return $http.get(apiEndpoint + 'api/v1.0/me/messages');
            }
        };
    }]);

</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2016-06-14
    • 2020-09-14
    • 2016-09-02
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多