在业务中心获取 azure key vault 的访问令牌时出错?

Error in getting access token for azure key vault in business central?

我正在尝试 POST 正文中包含不同参数的 HTTP 请求,但我收到消息 "oauth2 error AADSTS90014: The request body must contain the following parameter: 'grant_type' "。通过 Postman,我能够使用相同的参数获取访问令牌。我不知道我在这里做错了什么。我也附上了截图。

如果有人能帮助我。

JSONAddPair(vJsonObject, 'grant_type', 'client_credentials');
JSONAddPair(vJsonObject, 'client_id', 'client-id-here');
JSONAddPair(vJsonObject, 'client_secret', 'secret-here');
JSONAddPair(vJsonObject, 'scope', 'https://vault.azure.net/.default');
vJsonObject.WriteTo(Body);

URL := 'https://login.microsoftonline.com/' + DirectoryId + '/oauth2/v2.0/token';

vRequestContent.WriteFrom(Body);
vRequestContent.GetHeaders(vContentHeaders);
vContentHeaders.Clear();
vContentHeaders.Add('Content-Type', 'application/json');

vHttpRequestMessage.Method := 'POST';
vHttpRequestMessage.SetRequestUri(URL);
vHttpRequestMessage.Content := vRequestContent;

vHttpClient.Send(vHttpRequestMessage, vHttpResponseMessage);

vHttpResponseMessage.Content().ReadAs(ResponseText);
Message(ResponseText);

Post request through postman

error message

正如@juunas 所说,您应该post 形成数据,而不是JSON。试试这个:

URL := 'https://login.microsoftonline.com/' + DirectoryId + '/oauth2/v2.0/token';

vRequestContent.WriteFrom('grant_type=client_credentials&client_id=<client id here>&client_secret=<secret here>&scope=https://vault.azure.net/.default');
vRequestContent.GetHeaders(vContentHeaders);
vContentHeaders.Clear();
vContentHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');

vHttpRequestMessage.Method := 'POST';
vHttpRequestMessage.SetRequestUri(URL);
vHttpRequestMessage.Content := vRequestContent;

vHttpClient.Send(vHttpRequestMessage, vHttpResponseMessage);

vHttpResponseMessage.Content().ReadAs(ResponseText);
Message(ResponseText);

如果您遇到一些错误,例如不正确的密码,请url对您的整个http请求内容进行编码。

更新: 将请求正文更改为纯文本后,此问题已得到解决