Bre*_*red 6 c# asp.net oauth google-calendar-api dotnetopenauth
我试图通过使用DotNetOpenAuth从Google获取日历列表几天来绞尽脑汁.
我可以使用DotNetOpenAuth示例成功获取联系人列表.我已使用OpenId + OAuth将其与我的域集成.一切都很好,以获得联系人列表.
所以我从那里修改了代码以尝试检索日历列表,并且我一直收到401 Unauthorized错误.
我知道这是授权,因为我可以获得联系人列表.有没有人有一个代码示例如何使用Google的DotNetOpenAuth检索日历或日历事件?
谢谢
更新:
谢谢你的回复.我已经阅读了所有可以得到的东西.这是我到目前为止所做的
第1步:我在GoogleConsumer.cs中创建了一个新的GetCalendarEndPoint
private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("https://www.google.com/calendar/feeds/default", HttpDeliveryMethods.GetRequest);
Run Code Online (Sandbox Code Playgroud)
第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;
Run Code Online (Sandbox Code Playgroud)
第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;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,当我拨打以下电话时,我获得了401 Unauthorized
var calendars = GoogleConsumer.GetCalendars(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
Run Code Online (Sandbox Code Playgroud)
在触发进行此调用的方法之前,我还检查了State.GoogleAccess令牌是否存在,只需将其显示在我的屏幕上即可.
再次,如果我exectute
var calendars = GoogleConsumer.GetContacs(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
Run Code Online (Sandbox Code Playgroud)
然后它的工作原理??????? 谢谢你的帮助.
周末的大部分时间我都在经历同样的事情。
我认为在对 Fiddler 进行了大量摆弄之后,我找到了原因并找到了一个解决方案,虽然不漂亮,但似乎可行。我发现我可以通过将 DNOA 生成的 Uri 复制并粘贴到浏览器中来访问日历源,但在尝试编程访问时总是收到 401。这显然是因为 HttpWebRequest 的默认自动重定向行为会丢弃重定向尝试设置的任何 cookie。联系人提要在重定向期间不会设置任何 cookie,因此它不会受到影响。
当您第一次请求日历源时(即使使用正确构建和签名的 OAuth 请求),Google 也会使用包含 cookie 的重定向进行回复。如果您在提要请求时没有同时提供“日历 cookie”,那么当您尝试遵循提要重定向时,您将收到 401 未经授权。
这是来自 Google 的 cookie 设置标头:
HTTP/1.1 302 Moved Temporarily
Set-Cookie: S=calendar=y7AlfgbmcqYl0ugrF-Zt9A;Expires=Tue, 10-Jan-2012 03:54:20 GMT;Secure
Run Code Online (Sandbox Code Playgroud)
为了让它发挥作用,我正在做以下事情:
// 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1478 次 |
| 最近记录: |