简体   繁体   English

使用Google Calendar Feed的DotNetOpenAuth

[英]DotNetOpenAuth with Google Calendar Feed

I have been racking my brain for a few days trying to get a list of calendars from Google using DotNetOpenAuth. 我试图通过使用DotNetOpenAuth从Google获取日历列表几天来绞尽脑汁。

I can successfully get a list of contacts using the DotNetOpenAuth Samples. 我可以使用DotNetOpenAuth示例成功获取联系人列表。 I have integrated it with my domain using the OpenId+OAuth. 我已使用OpenId + OAuth将其与我的域集成。 Everything works great to get a list of contacts. 一切都很好,以获得联系人列表。

So from there I modified the code to try to retrieve a list of Calendars and I keep getting a 401 Unauthorized error. 所以我从那里修改了代码以尝试检索日历列表,并且我一直收到401 Unauthorized错误。

I know it is authorizing because I can get the contact list. 我知道这是授权,因为我可以获得联系人列表。 Does anyone have a code example how they are retrieving calendars or calendar events using the DotNetOpenAuth with Google??? 有没有人有一个代码示例如何使用Google的DotNetOpenAuth检索日历或日历事件?

Thanks 谢谢

Update: 更新:

Thanks for the response. 谢谢你的回复。 I have read everything I can get my hands on. 我已经阅读了所有可以得到的东西。 Here is what I have done so far 这是我到目前为止所做的

Step 1: I created a new GetCalendarEndPoint in the GoogleConsumer.cs 第1步:我在GoogleConsumer.cs中创建了一个新的GetCalendarEndPoint

private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("https://www.google.com/calendar/feeds/default", HttpDeliveryMethods.GetRequest);

Step 2: Next I created a new method GetCalendars patterned after the GetContacts Method in GoogleConsumer.cs - (Rebuilt the dll etc.) 第2步:接下来,我在GoogleConsumer.cs中的GetContacts方法之后创建了一个新方法GetCalendars - (重建了dll等)

    public static XDocument GetCalendars(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/) {
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }

            var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken);
            var response = consumer.Channel.WebRequestHandler.GetResponse(request);
            string body = response.GetResponseReader().ReadToEnd();
            XDocument result = XDocument.Parse(body);
            return result;

Step 3: In my Application I modified the ScopeURI to the the Calendar URI from GoogleConsumer as follows 第3步:在我的应用程序中,我将ScopeURI从GoogleConsumer修改为Calendar URI,如下所示

private IAuthenticationRequest GetGoogleRequest()
{
    Realm realm = Request.Url.Scheme + Uri.SchemeDelimiter + Global.GoogleTokenManager.ConsumerKey + "/";
    IAuthenticationRequest authReq = relyingParty.CreateRequest(GoogleOPIdentifier, realm);

    // Prepare the OAuth extension
    string scope = GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Calendar);
    Global.GoogleWebConsumer.AttachAuthorizationRequest(authReq, scope);

    // We also want the user's email address
    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    authReq.AddExtension(fetch);

    return authReq;
}

However, when I run the app I get 401 Unauthorized when I make the following call 但是,当我运行应用程序时,当我拨打以下电话时,我获得了401 Unauthorized

 var calendars = GoogleConsumer.GetCalendars(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);

I have also checked that the State.GoogleAccess token exists by simply displaying it on my screen before I trigger the method that makes this call. 在触发进行此调用的方法之前,我还检查了State.GoogleAccess令牌是否存在,只需将其显示在我的屏幕上即可。

Again, if I exectute 再次,如果我exectute

var calendars = GoogleConsumer.GetContacs(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);

then it works??????? 然后它的工作原理??????? Thanks for you help. 谢谢你的帮助。

I've been suffering through exactly the same thing for most of the weekend. 在周末的大部分时间里,我一直在遭受同样的痛苦。

I think that after much fiddling with Fiddler I've found the cause and have a solution which, although not pretty, seems to work. 我认为,在与Fiddler进行大量调整之后,我找到了原因并找到了一个解决方案,虽然不是很好,但似乎有效。 I found that I was able to access the calendar feed by copying and pasting the DNOA-generated Uri into a browser, but always got a 401 when attempting programmatic access. 我发现通过将DNOA生成的Uri复制并粘贴到浏览器中,我可以访问日历源,但在尝试编程访问时总是得到401。 This is apparently because the default auto-redirect behavior of HttpWebRequest discards any cookies that the redirect is attempting to set. 这显然是因为HttpWebRequest的默认自动重定向行为会丢弃重定向尝试设置的任何cookie。 The Contacts feed doesn't set any cookies during the redirect, so it is immune. “联系人”Feed在重定向期间未设置任何Cookie,因此它具有免疫力。

The first time you request a calendar feed (even with a properly constructed and signed OAuth request), Google replies with a redirect containing a cookie. 当您第一次请求日历Feed时(即使使用正确构建和签名的OAuth请求),Google也会回复包含Cookie的重定向。 If you don't present that "calendar cookie" at the same time as your feed request you will get a 401 Unauthorized when you attempt to follow the redirect to the feed. 如果您没有在Feed请求的同时提供“日历Cookie”,那么当您尝试按照重定向到Feed时,您将获得401 Unauthorized。

here's the cookie-setting header from Google: 这是来自Google的Cookie设置标题:

HTTP/1.1 302 Moved Temporarily
Set-Cookie: S=calendar=y7AlfgbmcqYl0ugrF-Zt9A;Expires=Tue, 10-Jan-2012 03:54:20 GMT;Secure

Here's what I'm doing to make it work: 这就是我正在做的工作:

// wc: WebConsumer
var calRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
// need to stop redirect to capture calendar cookie token:
calRequest.AllowAutoRedirect = false;
var calResponse = calRequest.GetResponse();
var redirectCookie = calResponse.Headers[System.Net.HttpResponseHeader.SetCookie];

var cookiedCalRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
cookiedCalRequest.Headers[System.Net.HttpRequestHeader.Cookie] = redirectCookie;
var calFeedResponse = cookiedCalRequest.GetResponse();

Have you read the Google Calendar data API documentation to make sure you have the right endpoints programmed in? 您是否阅读过Google日历数据API文档以确保编写了正确的端点? Have you also modified the code that acquires the access token to request access to Google Calendar in addition to Google Contacts? 除了Google通讯录之外,您是否还修改了获取访问令牌的代码以请求访问Google日历? The access token in the sample only gets Contacts permissions unless you change it. 除非您更改,否则示例中的访问令牌仅获取“联系人”权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM