asp.net - OneDrive for Business :"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: ' grant_type

标签 asp.net onedrive

我正在尝试将 OneDrive for Busines 集成到 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 请求以获取身份验证 token 。
这是第二页的代码:
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);
        }
    }

但是我收到以下错误而不是 Response 。其中包括 grant_type在网址中。我已经添加了(你可以 checkin 代码)。
我得到同样的错误同样的错误而不包括它。

这是错误
{"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}

请帮助知道哪里或什么地方出错了。
任何形式的帮助都是可观的

最佳答案

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

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

关于asp.net - OneDrive for Business :"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: ' grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430855/

相关文章:

c# - 使用 FormsAuthentication.SetAuthCookie 存储更多信息

c# - 在 IIS6 中启用通配符映射时 WCF 方法返回 404

javascript - OneDrive API 公开上传文件

c# - 如何查找Skydrive文件夹的文件夹ID

database - 使用 OneDrive 或 Dropbox 作为数据库

javascript - asp.net webforms 从用户控件注册 javascript

c# - Convert.ToString 不处理 null

asp.net - 在 ASP.NET 中使用链接和 anchor 切换可见性的最佳方法是什么?

wpf - 从 Windows 7 WPF 访问 Live SDK Skydrive

python-3.x - 身份验证 OneDrive Python API