OneDrive for Business :“invalid_request”,“error_de

2019-04-25 08:13发布

问题:

I m trying to integrate the OneDrive for Busines to a Web Form App. For this i am using the documentation given at this url https://dev.onedrive.com/auth/aad_oauth.htm In web Form App I have two Page First one is Login page which have a button for login In button login click i am making a GET Request to OneDrive for Business API using the following code

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;

When I Click on login button it is taking me to micorosoft login servie and sending me back to callback.aspx page with access code (Redirect URi configured on azure)

I got the access code On second page i am redeeming the access code and making a POST request to get the Authentication token Here is the code for the second page.

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

But instead of Response i am getting following error. Which say include the grant_type in url. I already added (u can check in code). Without including this also i am getting same error.

Here is error

{"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}

please help to know where , what geting wrong. Any kind of help will be appreciable Thanks a lot in advance

回答1:

You're adding the parameters to the request querystring. You have to post the data in the request body.

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


回答2:

use FormUrlEncodedContent instead of StringContent (form data post)

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


回答3:

Sharing for future readers because this error is not specific to OneDrive only but can arise in other Microsoft tools

I was getting this error when working with Microsoft Bot Framework's Skype bot. In my case the bot file the appId and appSecret was wrongly set to clientId and clientSecret

Changing the same to appId and appSecret fixed the issue.