我一直在努力让 Google Calendar API 与 DotNetOpenAuth 一起工作。我尝试了几种不同的方法,但我一直遇到以下错误:
The remote server returned an error: (401) Unauthorized.
我已经通过使用 Google Data API for .NET 中的 CalendarService 以及通过调整 DotNetOpenAuth 示例项目中的 Google Contacts 示例进行了尝试。
当我尝试使用 CalendarService 时,我通过设置身份验证令牌来设置身份验证服务,如下所示:
_service = new CalendarService("CalendarSampleApp");
_service.SetAuthenticationToken(accessToken);
在重新编写 Google 联系人示例时,我只是更改了端点以反映 Google 日历端点而不是 Google 联系人端点,如下所示:
private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("http://www.google.com/calendar/feeds/default/private/full", HttpDeliveryMethods.GetRequest);
public static XDocument GetCalendarEntries(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/)
{
if (consumer == null)
{
throw new ArgumentNullException("consumer");
}
var extraData = new Dictionary<string, string>() {
{ "start-index", startIndex.ToString(CultureInfo.InvariantCulture) },
{ "max-results", maxResults.ToString(CultureInfo.InvariantCulture) },
};
var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken, extraData);
// Enable gzip compression. Google only compresses the response for recognized user agent headers. - Mike Lim
request.AutomaticDecompression = DecompressionMethods.GZip;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16";
var response = consumer.Channel.WebRequestHandler.GetResponse(request);
string body = response.GetResponseReader().ReadToEnd();
XDocument result = XDocument.Parse(body);
return result;
}
当我运行它时,这是发送的请求:
正如我所说,在这两种情况下,我都会遇到相同的 401 错误。有没有人对我做错了什么有任何想法,或者如何进一步排除故障?