【问题标题】:Subscribe to Microsoft Office 365 Management Activity API: Authorization denied订阅 Microsoft Office 365 管理活动 API:授权被拒绝
【发布时间】:2016-06-25 09:18:50
【问题描述】:

我尝试订阅 Office 365 管理 API 并失败。

发布到https://manage.office.com/api/v1.0/{tennant-id}/activity/feed/sub scriptions/start?contentType=Audit.SharePoint 返回以下错误:

<Response [401]> {"Message":"Authorization has been denied for this request."}

使用本地 python 环境时。 我已经拥有访问令牌,我用它来打印 office 365 根网站并连接到 Azure Active Directory。 我怀疑错误出在我创建 Microsoft azure web 应用程序的方式上,因此我创建了一个新的 azure web 应用程序,它以相同的结果结束,当我创建“Native Client 应用程序”时也以相同的结果结束。 当我使用“网络应用程序 python 控制台”时,例如“pythonanywhere”,出现异常,出现以下错误: “无法连接到代理。套接字错误:隧道连接失败:403 Forbidden。”

我按照微软手册:https://msdn.microsoft.com/en-us/library/office/mt227394.aspx#StartSubscription

我的代码示例:

tid = {my tenant ID}
subscriptionHeader = { 'Content-Type': 'application/json; utf-8' , 'Authorization': 'Bearer %s' % (access_token)}
subscriptionBody = {"webhook" : {
       "address": {my site} } }
subscribtionPostUrl = "https*://manage.office.com/api/v1.0/%s/activity/feed/subscriptions/start?contentType=Audit.SharePoint" %(tid)
print ("posting to %s\n" % (subscribtionPostUrl) )
subscribtionPost = requests.post(subscribtionPostUrl, data=subscriptionBody, headers=subscriptionHeader)
print ("subscribtionPost %s\n" % (subscribtionPost ) )
print ("subscribtionPost %s\n" % (subscribtionPost.text ) )

【问题讨论】:

    标签: python azure python-requests jwt office365api


    【解决方案1】:

    我遇到了同样的问题,解决方案是在您请求令牌时使用 https://manage.office.com 作为资源(而不是 https://manage.office.com/ - 请注意末尾的正斜杠,您不应该包含它...)

    【讨论】:

    • 这也是我的问题。对 JWT 的请求成功:https://manage.office.com/https://manage.office.com,但随后使用带有斜杠的生成令牌失败。如果 MS 知道他们在做什么,并且 https://manage.office.com/ 确实是一个无效资源,那么对它的 JWT 请求应该会失败。相反,如果https://manage.office.com/ 是有效资源,则使用此排列的身份验证尝试应该会成功。 {"Message":"Authorization has been denied for this request."} 不透明且无用。
    【解决方案2】:

    根据您的错误信息:

    {"Message":"此请求的授权已被拒绝。"}

    您的 Azure AD 应用程序似乎没有足够的权限进行此操作。您可以尝试运行以下 powershell 脚本来升级您的 AD 应用程序的权限:

    #use the administrator account to sign in 
    Connect-MsolService
    $ClientIdWebApp = '{your_AD_application_client_id}'
    $webApp = Get-MsolServicePrincipal –AppPrincipalId $ClientIdWebApp
    #use Add-MsolRoleMember to add it to “Company Administrator” role).
    Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberType ServicePrincipal -RoleMemberObjectId $webApp.ObjectId
    

    同时,每个 access_token 都会在 3600 秒后过期,在我的测试过程中,只有当我的 access_token 过期时,我才会重现你的问题。

    【讨论】:

    • 感谢您的回答,我应该从哪里运行此脚本?当我与想要提升其权限的特定 Azuee AD 用户连接时,似乎我需要从 azure 运行此脚本?
    • 我设法解决了这个问题:问题出在我试图访问的资源中。按照文档重新编写代码后,我成功收到了这个答案: {"contentType":"Audit.SharePoint","status":"enabled","webhook":null} 将设法从这里继续,感谢您的帮助。
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多