php Azure Rest API给出AuthorizationFailed错误和invalid_request错误

osh3o9ms  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(44)

我尝试了来自不同文章的两个不同API,如下所示。
API 1**


的数据
此API正在工作,令牌正在生成。
然后在API的下一部分中,我使用生成的令牌来获取数据:



但它也给出了以下错误,如屏幕截图所示。

{
    "error": {
        "code": "AuthorizationFailed",
        "message": "The client '**************' with object id '**************' does not have authorization to perform action 'Microsoft.Web/sites/read' over scope '/subscriptions/**************' or the scope is invalid. If access was recently granted, please refresh your credentials."
    }
}

字符串
API 2**



但是在这个API中,甚至没有生成令牌,并给出以下错误

{
    "error": "invalid_request",
    "error_description": "AADSTS901002: The 'resource' request parameter is not supported. Trace ID: ************ Correlation ID: ************ Timestamp: 2023-12-28 07:17:52Z",
    "error_codes": [
        901002
    ],
    "timestamp": "2023-12-28 07:17:52Z",
    "trace_id": "************",
    "correlation_id": "************"
}


我搜索了但找不到解决方案。我想通过Rest API从Azure Active Directory获取数据。但这似乎不起作用。

ie3xauqp

ie3xauqp1#

我创建了一个Azure AD应用程序并授予了API权限,如下所示:
x1c 0d1x的数据
我使用以下参数生成了访问令牌

https://login.microsoftonline.com/TenantID/oauth2/token

client_id:ClientID
client_secret:ClientSecret
resource:https://management.azure.com
grant_type:client_credentials

字符串



使用上面生成的访问令牌,我尝试调用API,得到相同的错误

GET https://management.azure.com/subscriptions/xxxx/providers/Microsoft.Web/sites?api-version=2016-08-01



如果Azure AD应用程序没有访问API所需的权限/角色,则通常会发生此错误。

若要解决此错误,请确保为Azure AD应用程序分配Reader角色,如下所示:

  • 前往Azure门户->订阅->选择订阅->访问控制(IAM)->添加->添加角色分配->选择阅读器->选择成员->审核并分配 *

现在重新生成token,就可以成功调用API了:

  • 由于我的环境中没有任何站点,因此得到的结果为空 *

要生成访问令牌以调用Microsoft Graph API,请将**https://graph.microsoft.com**作为资源传递。

https://login.microsoftonline.com/TenantID/oauth2/token

client_id:ClientID
client_secret:ClientSecret
resource:https://graph.microsoft.com
grant_type:client_credentials

  • 由于您使用v1 endpoint生成访问令牌,因此resource必须分别为https://management.azure.comhttps://graph.microsoft.com
  • 如果使用v2 endpoint生成访问令牌,则必须分别将scope作为https://management.azure.com/.defaulthttps://graph.microsoft.com/.default传递。

相关问题