python - 将 Auth 烘焙到 Azure 函数应用程序中 : what's the canonical way?

标签 python azure azure-functions

我想制作一个函数应用程序,它返回事件发生之前的持续时间。这些事件存储在 Outlook 的共享日历中。

我可以使用图形浏览器通过以下网址访问共享日历:

https://graph.microsoft.com/v1.0/users/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f6c6e636a616b6e7d416e626a4f78676a7d6a4678607d64216c6062216e7a" rel="noreferrer noopener nofollow">[email protected]</a>/events?$select=subject,start

所以我知道系统的一部分是有效的。函数应用将获取下一个事件,如果距离事件发生还有 4 天,则返回 4

我不希望用户需要进行身份验证,我希望此信息对全世界可见。

将身份验证烘焙到函数中的正确*方法是什么?

*如果这样的概念有意义

最佳答案

您可以通过 client_credentials 流程来实现。请按照以下步骤操作: 1. 注册 Azure AD 应用程序并记下其应用程序 ID。授予 Microsoft Graph API 的读取日历权限: enter image description here enter image description here 请记住单击“为您的租户授予管理员同意”以完成权限授予过程。 完成此过程后,此应用将有权通过图形 API 读取您租户中所有用户的日历信息。

  • 为您的应用创建一个客户端 key ,并记下它,我们稍后将使用它: enter image description here
  • 尝试下面的 azure 函数代码:

    import logging
    import requests
    import json
    import azure.functions as func
    
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
    
        tenantID = "<your tenant ID>"
        account = "<account in your tenant you want to share event>"
        appId= "<app ID you just registered >"
        appSecret = "<app secret you just created>"
    
        authUrl = 'https://login.microsoftonline.com/'+ tenantID +'/oauth2/token'
        body = "grant_type=client_credentials&resource=https://graph.microsoft.com&client_id="+ appId +"&client_secret=" + appSecret
        headers = {'content-type': 'application/x-www-form-urlencoded'}
    
        r = requests.post(authUrl, data=body, headers=headers)
        access_token = json.loads(r.content)["access_token"]
    
        graphURL = "https://graph.microsoft.com/v1.0/users/"+account+"/events?$select=subject,start"
    
        result =requests.get(graphURL,
        headers={'Authorization': 'Bearer ' + access_token}
        )
    
        #get all event results here and you can finish your logic below:
        eventResult = result.content
    
    
        return func.HttpResponse(str(eventResult))
    

    结果: enter image description here 正如您所看到的,您可以从您指定的帐户获取事件。当然,你可以在函数中获取事件后执行自己的业务逻辑。希望能帮助到你 。

    关于python - 将 Auth 烘焙到 Azure 函数应用程序中 : what's the canonical way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59024043/

    相关文章:

    python - 使用 cv2 进行色彩空间转换

    python - 创建一个类来生成子图

    c# - Azure Functions v2 的 HttpClientFactory 的可用性

    azure - 访问 IIS 托管的 Web 应用程序,该应用程序通过 PowerShell 使用 AzureAD 凭据来使用 API

    Azure 托管代理 - Visual Studio 2017

    node.js - 如何在 Azure Functions 中进行路由?

    python - 使用 groupby 的一列创建带有 pandas 的 X 新列

    python - 通过python脚本调用ProxyCommand时“No such file or directory”

    c# - 使用 Azure Functions 进行 ILogger 依赖注入(inject)

    azure - 使用托管标识从逻辑应用程序进行身份验证来调用 Azure 函数