调用令牌的Microsoft Graph API会出现错误"AADSTS900144:请求正文必须包含以下参数:'grant_type' [英] Calling an Microsoft Graph API for token gives error "AADSTS900144: The request body must contain the following parameter: 'grant_type'

查看:336
本文介绍了调用令牌的Microsoft Graph API会出现错误"AADSTS900144:请求正文必须包含以下参数:'grant_type'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用Graph API URL

  https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token 

获取访问令牌,但得到以下响应.

  {错误":"invalid_request","error_description":"AADSTS900144:请求正文必须包含以下参数:'grant_type'.\ r \ n跟踪ID:5ff6b053-9011-4397-89ff-fdb6f31e4600 \ r \ n相关性ID:22509847-199d-4bd8-a083-b29d8bbf3139\ r \ n时间戳: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"} 

我有一个活跃的租户,已经注册了一个应用程序,并且我有一个上述应用程序的活跃用户,例如 user@tenant.onmicrosoft.com ;该用户具有所有角色(全局管理员).

请在下面找到邮递员的要求和回复.

解决方案:

您尝试的方式有误.您必须使用键-值对在邮递员上以 form-data 发送必需的参数,如下格式:

  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 =",["scope"] ="https://graph.microsoft.com/.default"});动态json;AccessTokenClass结果=新的AccessTokenClass();HttpClient客户端=新的HttpClient();var tokenResponse =等待客户端.SendAsync(tokenRequest);json =等待tokenResponse.Content.ReadAsStringAsync();结果= JsonConvert.DeserializeObject< AccessTokenClass>(json); 

使用的类:

 公共类AccessTokenClass{公共字符串token_type {get;放;}公共字符串expires_in {get;放;}公共字符串资源{get;放;}公共字符串access_token {get;放;}} 

您可以参考

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.

这篇关于调用令牌的Microsoft Graph API会出现错误"AADSTS900144:请求正文必须包含以下参数:'grant_type'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆