【问题标题】:Google Calendar API Redirect Exception issueGoogle Calendar API 重定向异常问题
【发布时间】:2013-12-11 12:57:05
【问题描述】:

我在访问 Google 日历时遇到了一个特殊问题 - 有时它会生成“重定向异常”,但大多数情况下它工作得很好。

我不知道如何重定向调用,根据我在搜索后阅读的内容,它应该由调用函数不可见的 Google API 本身处理(但显然不是。)

代码访问 Google 日历,获取所有辅助日历的列表以及每个日历具有的事件数。

API 版本 1.8.0.0 (http://code.google.com/p/google-gdata/downloads/list)

错误信息:

        Execution resulted in a redirect from https://www.google.com/calendar/feeds/tlf1juohv473hh0jhm046do5g9@group.calendar.google.com/private/full?gsessionid=MGsId0ULhyO_DkKlGa9DHw
        at Google.GData.Client.GDataRequest.Execute()
        at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
        at Google.GData.Client.GDataGAuthRequest.Execute()
        at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength)
        at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince)
        at Google.GData.Client.Service.Query(FeedQuery feedQuery)
        at GoogleDataTool.CalendarActions.GetSecondaryCalendars(Boolean IncludeEventCount, List`1& calendarlist) in D:\GoogleDataTool\classes\CalendarActions.cs:line 65

        Response.Message: "Stream was not readable."
        at Google.GData.Client.GDataRequest.Execute()
        at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
        at Google.GData.Client.GDataGAuthRequest.Execute()
        at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength)
        at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince)
        at Google.GData.Client.Service.Query(FeedQuery feedQuery)
        at GoogleDataTool.CalendarActions.GetSecondaryCalendars(Boolean IncludeEventCount, List`1& calendarlist) in  D:\GoogleDataTool\classes\CalendarActions.cs:line 84
        at GoogleDataTool.Program.DoActions() in D:\GoogleDataTool\Program.cs:line 34
        at GoogleDataTool.Program.Main(String[] args) in D:\GoogleDataTool\Program.cs:line 14
        at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
        at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        at System.Threading.ThreadHelper.ThreadStart()

代码(它只是收集集合中的日历、标题和事件计数):

        public bool GetSecondaryCalendars(out List<CalendarData> calendarlist)
    {
        Console.WriteLine( "Starting calendarlist retrieval..." );

        calendarlist = new List<CalendarData>();

        CalendarService myService = new CalendarService( "Calendar-application" );
        myService.setUserCredentials( "MyAccount@google.com", "MyPassword" );

        CalendarQuery cQuery = new CalendarQuery();
        cQuery.Uri = new Uri( "https://www.google.com/calendar/feeds/default/allcalendars/full" );

        try
        {
            CalendarFeed cFeed = (CalendarFeed)myService.Query( cQuery );

            foreach (CalendarEntry entry in cFeed.Entries)
            {
                // We don't want the primary calendar; just the secondary ones
                if (!entry.Id.Uri.ToString().Contains( HttpUtility.UrlEncode( "MyAccount@google.com" ) ))
                {
                    String calendarid = entry.Id.Uri.ToString().Substring( entry.Id.Uri.ToString().LastIndexOf( "/" ) + 1 );
                    calendarlist.Add( new CalendarData()
                    {
                        Text = entry.Title.Text,
                        Id = calendarid
                    } );
                }

                // Grab each calendar to count the number of events it has (not pretty, but I wish I could just ask the cFeed entries...)
                foreach (CalendarData calendar in calendarlist)
                {
                    FeedQuery query = new FeedQuery();

                    // Create the query object:
                    query.Uri = new Uri( string.Format( "https://www.google.com/calendar/feeds/{0}/private/full", calendar.Id ) );

                    // Tell the service to query:
                    AtomFeed calFeed = myService.Query( query );
                    calendar.EventCount = calFeed.Entries.Count;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine( ex.Message );
            return false;
        }
        return true;
    }

    public class CalendarData
    {
        public string Id { get; set; }
        public string Text { get; set; }
        public int EventCount { get; set; }
    }

欢迎提出任何建议。

【问题讨论】:

    标签: c# exception redirect google-calendar-api


    【解决方案1】:

    从我的观点来看,你正在互联网上工作 - 因此你应该计划在放弃之前至少尝试两次;-)
    因此,我会重试一次,然后引发最终用户可见的实际异常。

    【讨论】:

    • 这是个好主意,而且可能是我必须走的路;但它补偿了谷歌的 API 应该处理的东西。 :)
    • 我已经将返回完整日历列表的函数以及检索给定日历的所有事件的函数移到了一个单独的函数中,该函数在放弃之前尝试了六次。这似乎使它更可靠。不是很漂亮,但很有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多