我正在尝试将OneDrive for Busines集成到Web Form应用程序中. 为此,我正在使用此URL上给出的文档 dev.onedrive/auth/aad_oauth.htm 在Web Form App中,我有两个页面 第一个是登录页面,其中有一个用于登录的按钮 在按钮登录中,单击我正在使用以下代码向OneDrive for Business API发出GET请求
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 dev.onedrive/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("login.windows/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)
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)
我获得了访问代码 在第二页上,我正在兑换访问代码并发出POST请求以获取身份验证令牌 这是第二页的代码.
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="login.windows/common/oauth2/token"; public string Redirecturi = "localhost:51642/CallBack.aspx"; public string ResourcesId = "api.office/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.我已经添加了(您可以签入代码). 不包括此错误,我也会遇到相同的错误.
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.
这是错误
{"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
推荐答案您正在将参数添加到请求查询字符串中.您必须将数据发布到请求正文中.
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();更多推荐
OneDrive for Business:"invalid
发布评论