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

标签 python azure azure-devops-rest-api

在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/

    相关文章:

    python - 类型错误 : 'float' object not callable in Python

    python - 如何在多索引数据帧中选择数据并使结果数据帧具有适当的索引

    azure - SSL:如何生成可导出到 MMC 中的 .pfx 的 CSR?

    Azure DevOps 的 Powershell Invoke-RestMethod 未使用 PAT 进行身份验证

    azure - 如何在 C# 测试中从 azure devops(以前的 vsts)上的测试用例获取参数值?

    python - 在 Python 中循环工作时无法变得简单

    python - `Cannot open include file: ' apr_perms_set.h '` 执行 `pip install mod_wsgi` 时

    使用 OAuth 或证书进行 Azure 承载身份验证?

    从 Blob 复制 Azure 存储

    azure - 如何使用azure devops api或其他方法从拉取请求中获取目标分支?