如何用groovy-wslite调用Microsoft Graph REST API并获取访问令牌
解决Groovy调用Microsoft Graph API获取令牌的AADSTS900144错误
你的代码抛出AADSTS900144: The request body must contain the following parameter: 'client_id'错误,核心原因是Azure AD的OAuth2授权端点要求请求体使用application/x-www-form-urlencoded格式传递参数,而你原本用了JSON格式,导致服务端无法识别请求中的参数。
结合你发现的正确格式要求,修正后的完整代码如下:
def authorizeHost = "https://login.microsoftonline.com" def authorizePath = "/${azureSetting.getTenantID()}/oauth2/v2.0/authorize" try { RESTClient client = new RESTClient(authorizeHost) def params = [ "client_id": azureSetting.getClientID(), "scope": "https://graph.microsoft.com/.default", "response_type": "code" ] def response = client.post( path: authorizePath, ) { type "application/x-www-form-urlencoded" urlenc params } LOGGER.info("Success: " + (response.statusCode == 200)); LOGGER.info("Output: (" + response.contentType + ") " + response.text); } catch (RESTClientException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); LOGGER.info("Error: " + sw.toString()); }
关键修改说明:
- 移除授权路径末尾的
?:参数放在请求体中,无需通过URL参数传递 - 将请求内容类型从
ContentType.JSON改为"application/x-www-form-urlencoded",匹配Azure AD端点的要求 - 使用
urlenc方法代替json方法传递参数,确保参数以表单编码格式发送
内容的提问来源于stack exchange,提问作者N.D.H.Vu
相关产品推荐
相关产品推荐

