Azure Rest API身份验证

时间:2018-10-31 22:21:32

标签: azure msal azure-authentication azure-monitoring

我正在构建一个Android应用程序,该应用程序将访问Azure REST API并从天蓝色监控中读取一些数据。 我在身份验证过程中遇到问题,因为不确定是否可以使用MSAL库进行身份验证以访问Azure REST API?

1 个答案:

答案 0 :(得分:0)

在您提到的演示代码中,该资源为Microsoft图形。 如果要使用Azure服务管理API,我们需要将资源更改为https://management.azure.com。我们需要assign role to the registried Application

我不熟悉预览SDK,但是我们也可以通过以下方法来获取Azure管理API的访问令牌。

默认情况下,Azure门户中不显示V2应用程序。因此,我们需要同意许可。然后我们可以在Azure门户中找到它。

https://login.microsoftonline.com/{tenantId}/adminconsent?
client_id={clientId}
&state=12345
&redirect_uri={redirectUrl}

然后使用管理员帐户批准同意。之后,我们可以在Azure门户中找到V2应用程序,并将角色分配给应用程序。

enter image description here

enter image description here 从这个document,我们可以知道v2.0端点不支持OAuth 2.0 Resource Owner Password Credentials Grant

因此我们可以使用以下授权码来获取访问令牌。

1)获取授权码

https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize?
client_id={clientId}&response_type=code
&redirect_uri={redirect_uri}
&response_mode=query
&scope=https://management.azure.com/user_impersonation
&state=12345 

2)获取访问令牌

 https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token?
 scope=https://management.azure.com/.default
 &client_id={clientId} 
 &grant_type=authorization_code
 &redirect_uri={redirectUri}
 &code =AQABAAIAAAC5una0EUFgTIF8ElaxtWjT6o1ePh... 

enter image description here

测试访问令牌

enter image description here