【问题标题】:Using Google API service account get all domain users' calendar list使用 Google API 服务帐号获取所有域用户的日历列表
【发布时间】:2015-01-09 00:57:26
【问题描述】:

我创建了一个服务帐户并委派了域范围的权限。我正在尝试获取每个用户都可以访问的日历列表。有些用户的电子邮件可以拨打电话,有些则不能并返回无效请求错误。有人可以帮忙吗?

import httplib2

from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

"""Email of the Service Account"""
SERVICE_ACCOUNT_EMAIL = 'xxxxx@developer.gserviceaccount.com'

"""Path to the Service Account's Private Key file"""
SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'key.p12'

def createCalendarService(user_email):
  """Build and returns a service object authorized with the service accounts
  that act on behalf of the given user.

  Args:
    user_email: The email of the user.
  Returns:
    Calendar service object.
  """
  print "--------- current email %s -----------" % user_email
  f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
  key = f.read()
  f.close()

  http = httplib2.Http()
  storage = Storage('calendar.dat')
  credentials = storage.get()

  if credentials is None or credentials.invalid or credentials.access_token_expired:
    credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
      scope='https://www.googleapis.com/auth/calendar', sub=user_email)
    http = credentials.authorize(http)
    http.request.credentials.refresh(http)
    storage.put(credentials)
  else:
    http = credentials.authorize(http)
  return build('calendar', 'v3', http=http)

all_email_list = [###a list of emails]

for email in all_email_list:
  calendar_service = createCalendarService(email)
  page_token = None
  while True:
    calendar_list = calendar_service.calendarList().list(pageToken=page_token).execute()
    for calendar_list_entry in calendar_list['items']:
      print calendar_list_entry['summary']
      print calendar_list_entry['accessRole']
    page_token = calendar_list.get('nextPageToken')
    if not page_token:
      break

Traceback (most recent call last):
  File "google_calendar_api.py", line 52, in <module>
    calendar_service = createCalendarService(email)
  File "google_calendar_api.py", line 40, in createCalendarService
    http.request.credentials.refresh(http)
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 559, in refresh
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 728, in _refresh
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 757, in _do_refresh_request
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/util.py", line 129, in positional_wrapper
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 516, in new_request
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 728, in _refresh
  File "build/bdist.macosx-10.9-x86_64/egg/oauth2client/client.py", line 790, in _do_refresh_request
oauth2client.client.AccessTokenRefreshError: invalid_request

【问题讨论】:

    标签: python access-token google-api-client service-accounts


    【解决方案1】:

    只使用这个:

    f = open('PK12_File', 'rb')
    key = f.read()
    f.close()
        
    credentials = SignedJwtAssertionCredentials(service_account_name=client_email,private_key=key, scope=Calendar_SCOPE,sub="admin_account")
    
    http = httplib2.Http()
    http = credentials.authorize(http)
    
    return build('calendar', 'v3', http=http)

    【讨论】:

    • 您好 Hamza,感谢您的回复。我使用的代码与您发布的完全相同。没有运气。它适用于我域中某些用户的电子邮件,但大多数时候它只是失败了。我认为这可能与无效的访问密钥有关。有什么想法吗?
    • 我猜您已经允许该应用程序在域的管理控制台中用作服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    相关资源
    最近更新 更多