【问题标题】:OneDrive for Business :"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_typeOneDrive for Business :"invalid_request","error_description":"AADSTS90014:请求正文必须包含以下参数:'grant_type
【发布时间】:2015-10-04 12:29:28
【问题描述】:

我正在尝试将 OneDrive for Business 集成到 Web 表单应用程序中。
为此,我使用this url
提供的文档 在网络表单应用程序中,我有两个页面:
第一个是登录页面,其中有一个登录按钮
在登录按钮上单击我使用以下代码创建对 OneDrive for Business API 的 GET 请求:

HttpClient client = new HttpClient();
            Redirecturi = Uri.EscapeDataString(Redirecturi);
            string url = string.Format("https://login.windows.net/common/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}", ClienId, Redirecturi);
            var response = client.GetAsync(url);
            var json = response.Result.Content.ReadAsStringAsync();
            Label2.Text = json.Result;

当我单击登录按钮时,它会将我带到 micorosoft 登录服务并将我发送回带有访问代码的 callback.aspx 页面(在 azure 上配置重定向 URI)

我得到了访问代码。
在第二页上,我兑换了访问代码并发出 POST 请求以获取身份验证令牌。 这是第二页的代码:

private string BaseUri="https://login.windows.net/common/oauth2/token";
    public string Redirecturi = "http://localhost:51642/CallBack.aspx";
    public string ResourcesId = "https://api.office.com/discovery/";
    private string ClienId = "180c6ac4-5829-468e-.....-822405804862"; ///truncated//azure 
    private string ClientSecert = "G4TAQzD8d7C4...OE6m366afv8XKbTCcyXr4=";//truncated
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString[OAuthConstants.AccessToken]))
        {
            // There is a token available already. It should be the token flow. Ignore it.
            return;
        }
        if (!string.IsNullOrEmpty(Request.QueryString[OAuthConstants.Code]))
        {
            string _accessCode = Request.QueryString[OAuthConstants.Code];
            HttpClient client = new HttpClient();
           // BaseUri = Uri.EscapeDataString(BaseUri);
            Redirecturi = Uri.EscapeDataString(Redirecturi);
            ResourcesId = Uri.EscapeDataString(ResourcesId);
            string url = string.Format("{0}?client_id={1}&redirect_uri={2}&grant_type=authorization_code&client_secret={3}&code={4}&grant_type=authorization_code&resource={5}", BaseUri, ClienId, Redirecturi, ClientSecert, _accessCode, ResourcesId);
            var response = client.PostAsync(url, null);
            var json = response.Result.Content.ReadAsStringAsync();
            Response.Write(json);
        }
    }

但我收到以下错误,而不是响应。其中说在网址中包含grant_type。我已经添加了(您可以签入代码)。 我得到同样的错误同样的错误,但不包括它。

这是错误

{"error":"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: 2adb3a7f-ceb1-4978-97c4-3dc2d3cc3ad4\r\nCorrelation ID: 29fb11a0-c602-4891-9299-b0b538d75b5f\r\nTimestamp: 2015-07-15 09:58:42Z","error_codes":[90014],"timestamp":"2015-07-15 09:58:42Z","trace_id":"2adb3a7f-ceb1-4978-97c4-3dc2d3cc3ad4","correlation_id":"29fb11a0-c602-4891-9299-b0b538d75b5f","submit_url":null,"context":null}

请帮助了解哪里出了问题。
任何形式的帮助都将是可观的

【问题讨论】:

  • 有想过这个吗?因为我在尝试使用 Java 访问端点时遇到了同样的问题

标签: asp.net onedrive


【解决方案1】:

分享给未来的读者,因为此错误不仅仅针对 OneDrive,而是可能出现在其他 Microsoft 工具中

我在使用Microsoft Bot FrameworkSkype bot 时遇到此错误。就我而言,机器人文件the appId and appSecret was wrongly set to clientId and clientSecret

将其更改为 appIdappSecret 修复了该问题。

【讨论】:

    【解决方案2】:

    使用 FormUrlEncodedContent 代替 StringContent(表单数据发布)

    var formContent = new FormUrlEncodedContent(new Dictionary<string, string>
    {
        { "client_id", clientId },
        { "client_secret", clientSecret },
        { "code", authCode },
        { "redirect_uri", redirectUri },
        { "grant_type", "authorization_code" }
    });
    
    var response = await httpClient.PostAsync("https://login.microsoftonline.com/common/oauth2/token", formContent);
    

    【讨论】:

    • FormUrlEncodedContent 而不是 StringContent 有什么问题?您不需要自己定义编码类型,您可以传递键值对而不是自己连接字符串。
    【解决方案3】:

    您正在将参数添加到请求查询字符串中。您必须在请求正文中发布数据。

    var content = new StringContent(
                    "grant_type=authorization_code" +
                    "&client_id=" + ClienId +
                    "&redirect_uri=" + Redirecturi +
                    "&client_secret=" + ClientSecert +
                    "&code=" + _accessCode +
                    "&resource=" + ResourcesId,
                    Encoding.UTF8, 
                    "application/x-www-form-urlencoded");
    
    var response = httpClient.PostAsync(BaseUri, content);
    
    var result = response.Result.Content.ReadAsStringAsync();
    

    【讨论】:

    • 我在谷歌浏览器中使用邮递员,我也得到同样的错误
    • Sunil> 在 Google Chrome 的 Post Man 中需要: 1) 添加标题 "Content-Type: application/x-www-form-urlencoded" 2) 然后删除正文参数之间的所有空格!跨度>
    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2021-10-18
    • 2021-08-04
    • 2021-10-20
    相关资源
    最近更新 更多