【问题标题】:Is it possible to get access token through azure graph and use it to access azure storage accounts?是否可以通过 azure graph 获取访问令牌并使用它来访问 azure 存储帐户?
【发布时间】:2019-09-25 06:21:44
【问题描述】:

例如,我可以通过 getaccesstokencredentials(username, password) 通过图形 api 进行身份验证 我可以使用此令牌访问 Azure 吗? 目前我们可以使用管理库中的 usertokencredentials 和 applicationtokencredentials,然后一旦完成,您就可以创建 azure 类的实例。 Azure azure = Azure.authenticate(credentials).withdefaultsubscription。 我想知道我们是否可以使用 getaccesstokencredentials 中的令牌而不是 usertokentcredentials 和 applicationtokencredentials

【问题讨论】:

  • 能否提供调用graph api的代码或提供重新分级getaccesstokencredentials的文档?
  • 这是通过gazure graph获取访问令牌的代码
  • getAccessTokenFromUserCredentials(String uname, String pwd) 抛出异常 { AuthenticationContext ctx; AuthenticationResult res; ExecutorService 服务 = null;尝试 { service = Executors.newFixedThreadPool(1); ctx=new AuthenticationContext(AUTHORITY, false, service); Future future = context.acquireToken("graph.microsoft.com", appid, uname, pwd, null); res=future.get(); } res 结果; }
  • 删除了一些因为它很长
  • 我们使用以下代码在 azure 中进行身份验证: UserTokenCredentials creds = new UserTokenCredentials( CLIENT_ID, domain, username, password, AzureEnvironment.AZURE); Azure azureAuth = Azure.authenticate(creds).withDefaultSubscription();我正在考虑使用通过图形 api 生成的令牌而不是来自 usertokencredentials 的令牌,因为如果我使用 usertokencredentials 进行身份验证,我会遇到“invalid_grant”问题,但是在使用图形时它是成功的

标签: java azure azure-storage-account


【解决方案1】:

我们不能使用同一个访问令牌来调用graph api和调用api来管理Azure资源。因为graph api的资源url是https://graph.microsoft.com/,但是Azure管理rest api的资源url是https://management.azure.com/。更多详情请参考https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-api-authentication

另外,关于如何使用Azure AD访问Azure存储,请参考以下步骤:

  1. 为您的委托人添加角色分配。

  1. 获取令牌。

    public static String getToken() throws Exception {
        String TENANT_ID = "your tenant id or name, e4c9*-*-*-*-*57fb";
        String AUTHORITY = "https://login.microsoftonline.com/" + TENANT_ID;
        String CLIENT_ID = "your application id, dc17*-*-*-*a5e7";
        String CLIENT_SECRET = "the secret, /pG*32";
        String RESOURCE = "https://storage.azure.com/";
        String ACCESS_TOKEN = null;
        ExecutorService service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = null;
        try {
            context = new AuthenticationContext(AUTHORITY, false, service);
            ClientCredential credential = new ClientCredential(CLIENT_ID, CLIENT_SECRET);
            Future<AuthenticationResult> future = context.acquireToken(RESOURCE, credential, null);
            ACCESS_TOKEN = future.get().getAccessToken();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } finally {
            service.shutdown();
        }
        return ACCESS_TOKEN;
    }
    
  2. 访问 blob。

    public static void main(String[] args) throws Exception {
        String token = getToken();
        StorageCredentialsToken credentialsToken = new StorageCredentialsToken("storagetest789", token);
        CloudBlobClient blobClient = new CloudBlobClient(new URI("https://storagetest789.blob.core.windows.net/"), credentialsToken);
        CloudBlobContainer blobContainer = blobClient.getContainerReference("pub");
        CloudBlockBlob blockBlob = blobContainer.getBlockBlobReference("test1.txt");
        blockBlob.uploadText("mytest");
    }
    

更多详情请参考https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2021-01-29
    • 2020-03-17
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多