【问题标题】:Office365 REST API returns Unauthorized with C# ADALOffice365 REST API 返回未经授权的 C# ADAL
【发布时间】:2017-05-16 15:40:49
【问题描述】:

我正在构建一个 WPF 应用程序。首先获取事件,然后通过 O365 RestAPI 创建事件。

我可以通过以下方式获取事件:

result = await authContext.AcquireTokenAsync(graphResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
                    string today = DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture);
                    string graphRequest = String.Format(CultureInfo.CurrentCulture, "https://outlook.office365.com/api/v2.0/me/calendarview?startDateTime=" + today + "T00:00:00&endDateTime=" + today + "T23:59:00&$select=Subject,organizer,start,end,attendees&$orderby=start/datetime%20asc");
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphRequest);
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    request.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
                    HttpResponseMessage response = HttpClient.SendAsync(request).Result;

                    if (!response.IsSuccessStatusCode)
                        throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);

但是当我尝试创建事件时,我会收到使用相同令牌的“未经授权”。我的应用拥有读取和写入日历的权限。

这是我的代码:

string postBody = "{'Subject':" + "'Discuss the Calendar REST API'," +
                            "'Body':{ " +
                                          "'ContentType':'HTML'," +
                              "'Content': 'I think it will meet our requirements!'},"
                           + "'Start': { DateTime: '" + 
                              start + "',"
                                    + " 'TimeZone': 'W. Europe Standard Time'}," +
                               "'End':{'DateTime': '" + end + "'," +
                                    "'TimeZone': 'W. Europe Standard Time'},"
                                + "'Attendees':[{" +
                                "'EmailAddress':{"
                                 + "'Address': '" + MailTB.Text + "'"
                                + 
                                " },"
                                + "'Type': 'Required'}]}";


    var emailBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");
        AuthenticationContext authContext = new AuthenticationContext(authority, new FileCache());
        AuthenticationResult result = await authContext.AcquireTokenAsync(graphResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));

        Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
    string today = DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture);
    string graphRequest = String.Format(CultureInfo.CurrentCulture, "https://outlook.office365.com/api/v2.0/me/events");
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, graphRequest);
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    request.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
        HttpResponseMessage response = MainWindow.HttpClient.PostAsync(graphRequest, emailBody).Result;

        if (!response.IsSuccessStatusCode)
            throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);

【问题讨论】:

  • 详细的错误信息是什么?为这个问题更新令牌是否有帮助?
  • @FeiXue-MSFT 这就是我得到的:状态代码:401 未经授权的内容长度:0 日期:星期一,2017 年 5 月 15 日 14:02:51 GMT 服务器:Microsoft-IIS/8.5 WWW- Authenticate: Basic Realm="" X-FEServer: HE1PR0902CA0019 X-Powered-By: ASP.NET request-id: a95cc2b6-7e90-4fe7-8f26-24c13f570f29 我刷新了令牌,一切还是一样

标签: c# oauth-2.0 adal office365api


【解决方案1】:

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, graphRequest); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); request.Headers.Add("Prefer", "outlook.timezone=\"西欧标准时间\""); HttpResponseMessage 响应 = MainWindow.HttpClient.PostAsync(graphRequest, emailBody).Result;

您发送帖子请求的代码不会发送您设置的标头,因为所有标头都是为request 参数设置的,但您没有在帖子中使用它。

要使用 access_token 发送帖子,您可以参考以下代码:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Bearer", accessToken);
httpClient.DefaultRequestHeaders.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
HttpResponseMessage response = httpClient.PostAsync(graphRequest, emailBody).Result;

【讨论】:

  • 谢谢! emailBody 也有错误,但现在它正在运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多