菜鸟教程小白 发表于 2022-12-13 13:10:39

ios - 使用服务帐户在 IOS 中访问 Google 日历


                                            <p><p>我需要访问非公开的 Google 日历,而无需用户登录甚至拥有 Google 帐户。</p>

<p>我创建了一个使用服务帐户访问 Google 日历的 Android 应用:</p>

<pre><code>      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();
</code></pre>

<p>这包括:</p>

<ol>
<li>在 Google App 控制台中创建项目</li>
<li>创建服务帐号</li>
<li>授予服务帐户对 Google 日历的访问权限</li>
</ol>

<p>效果很好!</p>

<p>我需要在 IOS 中做同样的事情。我已经阅读了我能找到的关于这个主题的每一个问题/答案,并找到了非常不同的答案。许多人说 Google 不允许在 IOS SDK 中这样做,因为服务帐户旨在供基于服务器的应用程序使用。我不同意,因为我需要的功能在 Android 中可用。那么,现在呢?</p>

<p>用例是这样的:
我的 IOS 应用需要访问 Google 日历。如果您可以使用 OAuth,那部分并不太难。我对这种方法的问题是:</p>

<ul>
<li>要求用户拥有 Google 帐户。我的许多用户都是 Apple 用户。我不能要求他们获得 Google 帐户才能使用我的应用。</li>
<li>我无法公开日历。因此,我需要向每个新用户授予访问权限。我想我可以使用基于 Web 的应用程序来做到这一点,但这并不能解决问题(请参阅之前的问题 - 没有 Google 帐户)。</li>
</ul>

<p>我确实需要能够查询非公开 Google 日历中的事件,而无需用户需要 Google 帐户。解决方案是使用“服务帐户”(我认为)。 </p>

<p>我读到一个问题/答案说这是可能的,但从未发布过解决方案。 (<a href="https://stackoverflow.com/questions/27117095/how-to-list-google-calendar-events-without-user-authentication" rel="noreferrer noopener nofollow">How to list Google Calendar Events without User Authentication</a>)</p>

<p>帮助!!!!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>官方文档建议,如果您想处理 Calendar API(例如),您必须拥有 Google Apps for Work (<a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority" rel="noreferrer noopener nofollow">source</a>)。 </p>

<blockquote>
<p>If you have a Google Apps domain—if you use Google Apps for Work, for example—an administrator of the Google Apps domain can authorize an application to access user data on behalf of users in the Google Apps domain. For example, an application that uses the Google Calendar API to add events to the calendars of all users in a Google Apps domain would use a service account to access the Google Calendar API on behalf of users.</p>
</blockquote>

<p>一旦满足先决条件,您可以尝试根据您的实现仅调用日历 API 的 REST URL(因为文档中似乎没有 iOS 支持或可用示例)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用服务帐户在 IOS 中访问 Google 日历,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34573041/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34573041/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用服务帐户在 IOS 中访问 Google 日历