OneDrive for Business:" invalid_request"," error_description":" AADSTS90014:请求正文必须包含以下参数:' grant_type

时间:2015-07-15 12:48:15

标签: asp.net onedrive

我正在尝试将OneDrive for Busines集成到Web表单应用程序中。 为此我使用此URL https://dev.onedrive.com/auth/aad_oauth.htm给出的文档 在Web表单应用程序中,我有两个页面 第一个是登录页面,其中有一个登录按钮 在按钮登录中单击我正在使用以下代码向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);
        }
    }

但是我没有响应,而是出现了以下错误。其中说包括url中的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}

请帮助知道哪里,哪些错误。 任何形式的帮助都会很明显 非常感谢提前

3 个答案:

答案 0 :(得分:6)

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

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();

答案 1 :(得分:1)

使用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);

答案 2 :(得分:0)

  

为将来的读者共享,因为此错误不仅限于OneDrive,还可能在其他Microsoft工具中出现

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

将其更改为appIdappSecret可以解决此问题。