gpt4 book ai didi

python - 使用获取的 oauth2 token 访问用户的 Azure Blob 存储

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

在Azure Blob存储中,我需要的是当用户登录他的帐户时获取访问 token ,并使用此访问 token 来执行列表/上传/下载用户Blob存储中的文件。(类似于我们可以做的在 Dropbox/Google 云端硬盘中执行)。使用给定的请求用户身份验证我得到代码,

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id=client_id&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&response_mode=query&scope=openid%20offline_access%20https%3A%2F%2Fstorage.azure.com%2Fuser_impersonation&state=12345

代码用于使用以下请求获取 token

POST /{tenant}/oauth2/v2.0/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id=client_id&scope=openid%20offline_access%20https%3A%2F%2Fstorage.azure.com%2Fuser_impersonation&code=OAAABAAAAiL9Kn2Z27UubvWFPbm0gLWQJVzCTE9UkP3pSx1aXxUjq3n8b2JRLk4OxVXr...&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&grant_type=authorization_code&client_secret=client_secret

但是当我使用 https://account_name.blob.core.windows.net/container_name?restype=container&comp=list 调用 get 请求列出时,我收到服务器无法进行身份验证的请求。确保授权 header 的值格式正确,包括签名。如何使用获得的token访问blob存储中的文件?我们可以使用 python 来做到这一点吗?

最佳答案

如果您想通过Azure AD访问Azure Blob存储,请引用以下步骤:

  1. 注册 Azure AD 应用程序

  2. 配置 Azure 应用程序

    a.配置权限 enter image description here enter image description here

  3. 为用户配置 RABC 角色

az role assignment create \
--role "Storage Blob Data Contributor" \
--assignee <email> \
--scope "/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>
  • 获取 token A。获取代码
    https://login.microsoftonline.com/<tenant>/oauth2/v2.0/authorize?
    client_id=<>
    &response_type=code
    &redirect_uri=http://localhost:3000/
    &response_mode=query
    &scope=https://storage.azure.com/user_impersonation
    &state=12345
    b.获取 token
    Post     https://login.microsoftonline.com/<>/oauth2/v2.0/token
    client_id=<>
    &scope=https://storage.azure.com/user_impersonation
    &code=OAAABAAAAiL9Kn2Z27UubvWFPbm0gLWQJVzCTE9UkP3pSx1aXxUjq3n8b2JRLk4OxVXr...
    &redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
    &grant_type=authorization_code
    &client_secret=<>
  • 调用 Azure Blob Rest API
  • Get https://myaccount.blob.core.windows.net/mycontainer/myblob
    Headers :
    Authorization: Bearer <>
    x-ms-version: 2019-02-02

    enter image description here

    关于python - 使用获取的 oauth2 token 访问用户的 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59406391/

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