gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:50:37 24 4
gpt4 key购买 nike

我想制作一个函数应用程序,它返回事件发生之前的持续时间。这些事件存储在 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com