1

我一直在努力让 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;
}

当我运行它时,这是发送的请求:

http://www.google.com/calendar/feeds/default/private/full?oauth_token=1/kyuMER6kPiG13pOw5M4NoEE7AJdN2fE4jTKSgaMToT8&oauth_consumer_key=419892106817-obg9lu9j9ihpnlh2e2qa01ber4hmq5mh.apps.googleusercontent.com&oauth_nonce=NtfQzRV3&oauth_signature_method=HMAC-SHA1&oauth_signature=XEdf4ipECY2iBfdQfupD1IK1uFw%3D&oauth_version=1.0&oauth_timestamp=1358213228&start -index=1&max-results=5&gsessionid=TV7D3zHTc7T3l41Xqfn-ig

正如我所说,在这两种情况下,我都会遇到相同的 401 错误。有没有人对我做错了什么有任何想法,或者如何进一步排除故障?

4

1 回答 1

1

我认为您正在使用旧版本的库。请从以下网址下载最新的稳定版本: https ://code.google.com/p/google-api-dotnet-client/wiki/Downloads

您还应该考虑阅读 OAuth2 的文档: https ://code.google.com/p/google-api-dotnet-client/wiki/OAuth2

希望它可以帮助,埃亚尔

于 2013-01-23T22:59:40.767 回答