Azure刷新令牌获取AccessToken报错900144的解决方法
解决Azure访问令牌生成错误(Error Code: 900144)
错误原因:请求格式不匹配。你设置了Content-Type为application/x-www-form-urlencoded,但却将请求体转换成JSON格式传递,而Azure AD的令牌端点要求该类型请求使用表单键值对格式,而非JSON,这就导致了请求体必须包含以下参数: '{name}'的错误。
修正方案
直接移除ConvertTo-Json的转换操作,Invoke-RestMethod会自动将哈希表格式的$body转换为符合要求的表单格式。
修正后的完整代码:
param ( $RefreshToken, $tenantId, $clientId, $clientSecret ) [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 # Azure token endpoint $tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" # Prepare the body of the request $body = @{ grant_type = "refresh_token" client_id = $clientId client_secret = $clientSecret refresh_token = $RefreshToken scope = "499b84ac-1321-427f-aa17-267ca6975798/.default" } # Make the POST request $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body # Output the response $response
补充说明
如果需要显式指定Content-Type,也可以添加-ContentType参数,效果一致:
$response = Invoke-RestMethod -Uri $tokenUrl -Method Post -ContentType "application/x-www-form-urlencoded" -Body $body
内容的提问来源于stack exchange,提问作者user10434309
相关产品推荐
相关产品推荐

