使用服务帐户在iOS中访问Google日历

问题描述:

我需要访问非公开Goog​​le日历,而不需要用户登录甚至没有Google帐户。使用服务帐户在iOS中访问Google日历

我创建了一个Android应用程序,使用一个服务帐户访问谷歌日历:

 GoogleCredential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(jsonFactory) 
       .setServiceAccountId(serviceAccountID) 
       .setServiceAccountScopes(scopes) 
       .setServiceAccountPrivateKeyFromP12File(licenseFile) 
       .build(); 
     com.google.api.services.calendar.Calendar.Builder builder = new com.google.api.services.calendar.Calendar.Builder(httpTransport, jsonFactory, credential); 
     builder.setApplicationName(appName); 
     com.google.api.services.calendar.Calendar client = builder.build(); 

     com.google.api.services.calendar.Calendar.Events.List list = client.events().list(calendarID); 
     list.setMaxAttendees(maxAttendees); 
     list.setTimeZone(timeZone); 
     list.setTimeMin(startTime); 
     list.setTimeMax(endTime); 
     list.setOrderBy(orderBy); 
     list.setShowDeleted(showDeleted); 
     list.setSingleEvents(true); 
     Events events = list.execute(); 

这包括:

  1. 建立在谷歌应用程序控制台
  2. 项目创建服务帐户
  3. 授予服务帐户访问Google日历的权利

它工作的很棒!

我需要在IOS中做同样的事情。我已经阅读了关于这个主题的每一个问题/答案,并且发现了很多不同的答案。许多人表示Google不允许在IOS SDK中使用这个功能,因为服务帐户旨在供基于服务器的应用程序使用。我不同意,因为我需要的功能在Android中可用。那么现在怎么办?

用例如下: 我的IOS应用需要访问Google日历。如果您可以使用OAuth,那么这一部分不会太难。我使用这种方法的问题是:

  • 需要用户拥有一个Google帐户。我的许多用户都是Apple-Only。我无法要求他们获取Google帐户只是为了使用我的应用。
  • 我无法公开日历。所以,我需要为每个新用户提供访问权限。我想我可以使用基于Web的应用程序来做到这一点,但这不能解决问题(请参阅上一个问题 - 没有Google帐户)。

我真的需要能够在非公开Goog​​le日历中查询事件,而无需用户使用Google帐户。解决方案是使用“服务帐户”(我认为)。

我看了一个提问/回答认为这是可能的,但解决方案从来没有公布。 (How to list Google Calendar Events without User Authentication

HELP !!!!

官方文档表明,如果您想处理Calendar API(例如),则必须拥有Google Apps for Work(source)。

如果您拥有Google Apps域(例如,如果您使用Google Apps for Work),Google Apps域的管理员可以授权应用程序代表Google Apps域中的用户访问用户数据。例如,使用Google Calendar API将事件添加到Google Apps域中所有用户的日历的应用程序将使用服务帐户代表用户访问Google Calendar API。

一旦满足前提条件,您可以尝试根据您的实现调用Calendar API的REST URL(因为似乎没有iOS支持或文档中提供的示例)。

+0

实际上,我在Google Apps for Work中使用Google Apps域。所以,我认为先决条件已经得到满足。你可以给我一些示例代码来展示如何使用REST URL。我已经尝试使用带有以下GET的高级REST客户端(Chrome插件)。我尝试了以下尝试失败的最大次数:GET https://www.googleapis.com/calendar/v3/calendars/heathwallace.com_gpupieshkuc85dd832m9gjrh9g%40group.calendar.google.com/events?key={YOUR_API_KEY}。我迷路了。我需要一个代码示例或上面讨论的工具的屏幕截图。 – JustLearningAgain

+0

您可以在REST中查看示例API调用的服务帐户页面。高级REST客户端可能有点棘手,因为REST调用需要授权。希望这将有助于你的需要。 – adjuremods

+0

你有任何示例代码显示如何使这项工作?我看过你引用的页面(阅读你的文章之前和之后)。问题是这似乎很复杂。这里的代码适用于Google云端硬盘,而不是日历,尽管我知道它们非常相似。我真的很喜欢显示所有步骤的简单方法。即使你的回答方向不同,如果我能确保我(和其他所有人都有这个问题)有一个简单的源代码示例,可以将它标记为答案。谢谢!!! – JustLearningAgain