DotNetOpenAuth和Google日历出现401(未经授权)错误

时间:2013-01-15 04:19:01

标签: google-api google-calendar-api dotnetopenauth google-api-dotnet-client

我现在一直在修补谷歌日历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错误。有没有人对我做错了什么,或者如何进一步排除故障有任何想法?

1 个答案:

答案 0 :(得分:1)

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

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

希望它可能有所帮助, 的Eyal