我正在调用一个图形API URL
https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token
以获得访问令牌,但我将得到以下响应。
{
"error": "invalid_request",
"error_description": "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: 5ff6b053-9011-4397-89ff-fdb6f31e4600\r\nCorrelation ID: 22509847-199d-4bd8-a083-b29d8bbf3139\r\nTimestamp: 2020-04-01 11:14:00Z",
"error_codes": [
900144
],
"timestamp": "2020-04-01 11:14:00Z",
"trace_id": "5ff6b053-9011-4397-89ff-fdb6f31e4600",
"correlation_id": "22509847-199d-4bd8-a083-b29d8bbf3139",
"error_uri": "https://login.microsoftonline.com/error?code=900144"
}
我有一个活动的tenantid,我注册了一个应用程序,还有一个上面的应用程序的活动用户,比如user@tenant.onmicrosoft.com
;该用户拥有所有的角色(Global )。
请参阅以下邮差的要求及回应。PostmanSnap
此外,我还按照https://learn.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=http中的建议授予API权限。
发布于 2020-04-01 14:05:30
问题:我已经成功地复制了您的错误。如下所示:
解决方案:
你想错了。您必须在邮递员上以form-data
格式发送所需的参数,key-value pairs
格式如下:
grant_type:client_credentials
client_id:b6695c7be_YourClient_Id_e6921e61f659
client_secret:Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=
scope:https://graph.microsoft.com/.default
代码片段:
//Token Request End Point
string tokenUrl = $"https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/v2.0/token";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
//I am Using client_credentials as It is mostly recommended
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
["client_id"] = "b6695c7be_YourClient_Id_e6921e61f659",
["client_secret"] = "Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=",
["scope"] = "https://graph.microsoft.com/.default"
});
dynamic json;
AccessTokenClass results = new AccessTokenClass();
HttpClient client = new HttpClient();
var tokenResponse = await client.SendAsync(tokenRequest);
json = await tokenResponse.Content.ReadAsStringAsync();
results = JsonConvert.DeserializeObject<AccessTokenClass>(json);
使用的类:
public class AccessTokenClass
{
public string token_type { get; set; }
public string expires_in { get; set; }
public string resource { get; set; }
public string access_token { get; set; }
}
您可以参考正式文件
希望能帮上忙。如果你仍然有任何顾虑,可以随意分享。
https://stackoverflow.com/questions/60970010
复制相似问题