401 (Unauthorized) Error with DotNetOpenAuth and Google Calendar

我的梦境 提交于 2019-12-24 16:25:17

问题


I've been tinkering for a while now with getting the Google Calendar API working with DotNetOpenAuth. I've tried going about it in a few different ways, and I keep running up against the following error:

The remote server returned an error: (401) Unauthorized.

I've tried it both by using the CalendarService in the Google Data API for .NET, and by adapting the Google Contacts sample in the DotNetOpenAuth samples project.

When I tried using the CalendarService, I set up my service for authentication by setting the authentication token like so:

_service = new CalendarService("CalendarSampleApp");
_service.SetAuthenticationToken(accessToken);

When reworking the Google Contacts sample, I simply changed the endpoint to reflect the Google Calendar endpoint instead of the Google Contacts endpoint, like so:

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;
}

When I run this, here's the request that gets sent:

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

As I said, in either of these cases, I'm getting the same 401 error. Does anyone have any ideas on what I am doing wrong, or how to troubleshoot further?


回答1:


I think you are working with an old version of the library. Please download the latest stable release from: https://code.google.com/p/google-api-dotnet-client/wiki/Downloads

And you should also consider reading OAuth2's documentation: https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2

Hope it may help, Eyal



来源:https://stackoverflow.com/questions/14331108/401-unauthorized-error-with-dotnetopenauth-and-google-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!