【问题标题】:Access Azure Blob storage of a user using oauth2 token obtained使用获得的 oauth2 令牌访问用户的 Azure Blob 存储
【发布时间】:2020-04-11 20:30:02
【问题描述】:

在 Azure blob 存储中,我需要在用户登录其帐户时获取访问令牌,并使用此访问令牌执行列表/上传/下载用户 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

代码用于使用以下请求获取令牌

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 调用获取请求以列出列表时,我得到了server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature。如何使用获得的令牌访问 blob 存储中的文件?我们可以使用 python 做到这一点吗?

【问题讨论】:

    标签: python azure azure-devops-rest-api


    【解决方案1】:

    如果要使用 Azure AD 访问 Azure Blob 存储,请参考以下步骤:

    1. 注册 Azure AD 应用程序

    2. 配置 Azure 应用程序

      一个。配置权限

    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>
    
    1. 获取令牌 一种。获取代码
      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
      
      湾。获取令牌
      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=<>
      
    2. 调用 Azure blob rest api
    Get https://myaccount.blob.core.windows.net/mycontainer/myblob
    Headers : 
                Authorization: Bearer <>
                x-ms-version: 2019-02-02
    
    

    【讨论】:

    • 我想访问一个用户帐户,该用户帐户允许使用他的 azure 存储帐户使用他授予权限时获得的令牌来上传/删除/下载文件。可以设置 RBAC,但我不能要求用户在他的帐户中为我设置该角色权限。
    • @subin_john_ 根据情况,你可能有两种选择。一是您可以要求用户为您创建一个服务主体,并为 sp 设置Storage Blob Data Contributor 角色。然后就可以使用sp访问存储了。另一种是您可以要求用户为您创建一个 sas 令牌,然后您可以使用 sas 令牌访问存储。
    • 好的,这样试试,谢谢
    • @subin_john_ 您还有其他顾虑吗?如果您没有其他问题,请accept the answer。它可能会帮助更多有类似问题的人。
    猜你喜欢
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2016-12-26
    • 2019-06-13
    • 1970-01-01
    • 2021-07-08
    • 2021-11-20
    • 2011-09-06
    相关资源
    最近更新 更多