問題描述
我正在調(diào)用 Graph API URL
https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token
獲取訪問令牌,但我收到以下響應.
<代碼>{錯誤":無效請求","error_description": "AADSTS900144: 請求正文必須包含以下參數(shù):'grant_type'. 跟蹤 ID: 5ff6b053-9011-4397-89ff-fdb6f31e4600 相關 ID: 22509847-199d-4bd8-a083-b29d8bbf3139 時間戳:2020-04-01 11:14:00Z",錯誤代碼":[900144],"時間戳": "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"}
我有一個活動的租戶 ID,我有一個注冊的應用程序,并且我有一個上述應用程序的活動用戶說 user@tenant.onmicrosoft.com
;該用戶具有所有角色(全局管理員).
請在下方找到 Postman 的請求和響應.
解決方案:
你在嘗試錯誤的方式.您必須使用 key-value 對
在郵遞員的 form-data
中發(fā)送所需的參數(shù),格式如下:
grant_type:client_credentialsclient_id:b6695c7be_YourClient_Id_e6921e61f659client_secret:Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=范圍:https://graph.microsoft.com/.default
代碼片段:
//令牌請求端點字符串 tokenUrl = $"https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/v2.0/token";var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);//我正在使用 client_credentials 作為它主要推薦tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>{["grant_type"] = "client_credentials",["client_id"] = "b6695c7be_YourClient_Id_e6921e61f659",["client_secret"] = "Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=",["范圍"] = "https://graph.microsoft.com/.default"});動態(tài)json;AccessTokenClass 結果 = new AccessTokenClass();HttpClient 客戶端 = 新 HttpClient();var tokenResponse = await client.SendAsync(tokenRequest);json = 等待 tokenResponse.Content.ReadAsStringAsync();結果 = JsonConvert.DeserializeObject(json);
使用的類:
公共類AccessTokenClass{公共字符串 token_type { 獲取;放;}公共字符串 expires_in { 獲取;放;}公共字符串資源 { 獲取;放;}公共字符串 access_token { 獲取;放;}}
您可以參考 官方文檔
希望這會有所幫助.如果您仍有任何疑慮,請隨時分享.
I am calling a Graph API URL
https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token
to get an access token but I am getting the following response.
{
"error": "invalid_request",
"error_description": "AADSTS900144: The request body must contain the following parameter: 'grant_type'.
Trace ID: 5ff6b053-9011-4397-89ff-fdb6f31e4600
Correlation ID: 22509847-199d-4bd8-a083-b29d8bbf3139
Timestamp: 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"
}
I have an active tenantid, I have an application registered, and I have an active user for the above application say user@tenant.onmicrosoft.com
; that user has ALL the roles (Global Administrator).
Please find below Postman's request and Response. PostmanSnap
Also I have given API permission as suggested in https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=http
Problem: I have successfully reproduced your error. As you seen below:
Solution:
You are trying in wrong way. You have to send required parameter in form-data
on postman with key-value pairs
like below format:
grant_type:client_credentials
client_id:b6695c7be_YourClient_Id_e6921e61f659
client_secret:Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=
scope:https://graph.microsoft.com/.default
Code Snippet:
//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);
Class Used:
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; }
}
You could refer to Official document
Hope that would help. If you still have any concern feel free to share.
這篇關于為令牌調(diào)用 Microsoft Graph API 會出現(xiàn)錯誤“AADSTS900144:請求正文必須包含以下參數(shù):“grant_type"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!