0

サービス アカウントを作成し、ドメイン全体の権限を委任しています。各ユーザーがアクセスできるカレンダーのリストを取得しようとしています。一部のユーザーの電子メールは呼び出しを行うことができ、一部のユーザーは呼び出しを行うことができず、無効な要求エラーを返します。誰でもそれを助けることができますか?

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
4

1 に答える 1

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)

于 2014-11-12T09:08:34.843 に答える