【问题标题】:Google Calendar API - service account - without google apps domain谷歌日历 API - 服务帐户 - 没有谷歌应用域
【发布时间】:2015-01-21 18:29:36
【问题描述】:

是否可以在没有 Google Apps 域的情况下将 Google Calendar API v3 与服务帐户一起使用?

基本上我想要做的是使用 python 脚本在我自己的日历上创建一个事件,而不需要脚本提示我输入用户和密码......有人知道吗?

我使用了这段代码,但没有运气:

import pprint
import pytz
import httplib2
import requests

from datetime import datetime, timedelta
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

with open('/home/xpto/service_account_certificate.pem', 'rb') as f:
  key = f.read()

service_account_name = '000000000000-j...@developer.gserviceaccount.com'

credentials = SignedJwtAssertionCredentials(
    service_account_name,
    key,
    scope=['https://www.googleapis.com/auth/calendar',
        'https://www.googleapis.com/auth/calendar.readonly'],
    sub='xpto@gmail.com')

http = httplib2.Http()
http = credentials.authorize(http)

service = build(serviceName='calendar', version='v3', http=http)

我收到此错误:

auth2client.client.AccessTokenRefreshError:未授权客户端:请求中的未授权客户端或范围

【问题讨论】:

  • 顺便说一句 - 你有一个 python 脚本提示输入用户和密码并能够修改用户的日历吗?我很想看看!

标签: python google-api google-calendar-api google-api-python-client service-accounts


【解决方案1】:

是的 - 只需创建一个服务帐户,这将为您提供该帐户的电子邮件地址。

然后,只需与该电子邮件地址共享您的日历,为其提供所需的任何权限。

【讨论】:

  • 好吧,它不起作用。我收到此错误:auth2client.client.AccessTokenRefreshError:authorized_client: Unauthorized client or scope in request.
  • 我有一个适用于 scope = calendar.readonly 的脚本。当然,这不会让您创建事件。另一种可能性是使用 api 调用创建服务帐户拥有的日历,然后与您的帐户共享。
  • 谢谢。阅读这篇文章,我能够解决我的问题:stackoverflow.com/questions/12252644/…。因此,我没有使用服务帐户,而是使用了普通帐户并使用该帖子末尾的脚本来检索和写入访问令牌。我已经使用了一个星期了,还没有过期......但是我无法找到令牌是否会过期以及何时过期。
【解决方案2】:

向服务帐户授予对 Google 资源的访问权限有效。就我而言,我试图使用服务帐户将文件推送到谷歌驱动器上。通过将我的谷歌驱动器上的文件夹的访问权限授予服务帐户(电子邮件),我能够查看文件并在我的帐户下的谷歌驱动器上创建文件。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 2017-12-24
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2013-05-11
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多