在尝试创建日历事件时拒绝权限(403)

时间:2016-01-19 08:59:26

标签: javascript azure-active-directory adal office365api microsoft-graph

我正在尝试通过JavaScript与adal.js和jQuery(OAuth隐式流程)集成到Office365 API,但我在尝试为我的用户创建日历事件时遇到问题。当检索电子邮件和日历事件时,我现有的代码工作正常,但当我尝试创建日历事件时,我一直得到“403 - 禁止”响应。

代码是http://oauth.idippedut.dk/oauth.html的实时和工作。我正在https://outlook.office.com/api/v2.0/me/events访问Office 365 API端点。

我在Office365 / Azure租户Active Directory中对应用程序的“委派权限”的配置如下: enter image description here

Office365 / Azure租户Active Directory中应用程序的“应用程序权限”配置如下: enter image description here

jQuery请求是这样的:

var event = {
    "Subject": "Discuss the Calendar REST API",
    "Body": {
        "ContentType": "HTML",
        "Content": "I think it will meet our requirements!"
    },
    "Start": {
        "DateTime": "2016-01-21T18:00:00",
        "TimeZone": "Pacific Standard Time"
    },
    "End": {
        "DateTime": "2016-01-21T19:00:00",
        "TimeZone": "Pacific Standard Time"
    },
    "Attendees": [
        {
            "EmailAddress": {
                "Address": "jesper@lundstocholm.dk",
                "Name": "Janet Schorr"
            },
            "Type": "Required"
        }
    ]
};

// Create calendar events
jQuery.ajax({
    type: 'POST',
    url: postCalenderEndpoint,
    data: JSON.stringify(event),
    contentType: "application/json",
    headers: {
        'Accept': 'application/json',
        'Authorization': 'Bearer ' + token,
    },

}).done(function (data) {
    //alert(JSON.stringify(data));
}).fail(function (err) {
    jQuery("#loginMessage").text('Error calling REST endpoint: ' + err.statusText + '\n' + err.responseText);
});

jQuery的配置是这样的:

var resource = 'https://outlook.office.com';
var postCalenderEndpoint = 'https://outlook.office.com/api/v2.0/me/events';
var clientID = '28a707a5-0f11-4d93-8b88-6a918544da14';
var tenantName = '365projectum.onmicrosoft.com';
var authContext = new AuthenticationContext({
    instance: 'https://login.microsoftonline.com/',
    tenant: tenantName,
    clientId: clientID,
    postLogoutRedirectUri: window.location.origin,
    cacheLocation: 'localStorage'
});

结果HTTP请求是这样的:

Host: outlook.office.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
Authorization: Bearer <my token>
Referer: http://oauth.idippedut.dk/oauth.html
Content-Length: 386
Origin: http://oauth.idippedut.dk
Connection: keep-alive

{"Subject":"Discuss the Calendar REST API","Body":{"ContentType":"HTML","Content":"I think it will meet our requirements!"},"Start":{"DateTime":"2016-01-21T18:00:00","TimeZone":"Pacific Standard Time"},"End":{"DateTime":"2016-01-21T19:00:00","TimeZone":"Pacific Standard Time"},"Attendees":[{"EmailAddress":{"Address":"jesper@lundstocholm.dk","Name":"Janet Schorr"},"Type":"Required"}]}

我真的很困惑为什么我得到403,因为一切都应该正确设置。

任何帮助将不胜感激: - )

/的Jesper

3 个答案:

答案 0 :(得分:2)

您为Microsoft Graph配置了委派权限,但调用了Outlook端点。您需要执行以下任一操作: 1.更改您的应用程序配置以获得Outlook / Office 365 Exchange Online的委派权限。 2.更改您的应用程序以使用Microsoft Graph端点(graph.microsoft.com),即https://graph.microsoft.com/v1.0/me/events并保留当前的应用程序配置。

答案 1 :(得分:1)

尝试&#34; https://graph.microsoft.com&#34;作为获得(右)令牌的资源。

致以最诚挚的问候,

AJ

答案 2 :(得分:0)

您最初是否注册了申请权限的应用程序&#34;阅读用户和共享日历&#34;,然后再添加权限&#34;拥有对用户日历的完全访问权限&#34;?如果是,您可能处于用户同意前一个许可的情况,并且由于该同意已到位,因此永远不会要求他们同意您添加的新权限。这可以解释为什么你的应用可以阅读,但无法写。

只有在您添加新权限之前,并且仅在用户实际同意的情况下,您才会遇到同意的用户。如果您将应用程序注册为管理员,并且与管理员在同一租户中的用户登录,则用户无需同意。如果您将应用程序注册为普通用户,或者该应用程序是多租户应用程序,则用户必须同意。

如果两者中的任何一个都是这种情况,那么查看这是否是问题的简单方法是尝试将该应用程序用作之前未同意的全新用户。此新用户将同意应用程序请求的所有权限。请注意,如果这是管理员同意的应用,那么您需要一个新的净租户才能同意。

如果这样做可以解决问题,那么您需要让现有用户完成使用prompt = consent参数发送新OAuth授权请求的步骤,以便他们再次同意。

相关问题