简体   繁体   English

Python Google Admin SDK 403错误

[英]Python Google Admin SDK 403 Error

I am attempting to retrieve all the members of a group but am receiving an error. 我试图检索组的所有成员但收到错误。

Here is my code: 这是我的代码:

# -*- coding: utf-8 -*-

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/admin-directory_v1-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/admin.directory.group.member.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
CRED_SAVE = 'cred_save.json'
APPLICATION_NAME = 'Directory API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   CRED_SAVE)

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def get_remote_users(service, http, group="all.faculty@myorg.jp"):
    request = service.members().list(groupKey=group).execute()


def main():
    """Shows basic usage of the Google Admin SDK Directory API.

    Creates a Google Admin SDK API service object and outputs a list of first
    10 users in the domain.
    """

    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('admin', 'directory_v1', http=http)

    users = get_remote_users(service, http)

    if not users:
        print('No users in the domain.')
    else:
        print('Users:')
        for user in users:
            print('{0} ({1})'.format(user['primaryEmail'],
                user['name']['fullName']))


if __name__ == '__main__':
    main()

Here is the error: 这是错误:

Traceback (most recent call last):
  File "/Users/user/Documents/workspace/module/groups_members.py", line 84, in <module>
    main()
  File "/Users/user/Documents/workspace/module/groups_members.py", line 72, in main
    users = get_remote_users(service, http)
  File "/Users/user/Documents/workspace/module/groups_members.py", line 58, in get_remote_users
    request = service.members().list(groupKey=group).execute()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/googleapiclient/http.py", line 832, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/groups/all.faculty%40myorg.jp/members?alt=json returned "Insufficient Permission">

Any idea why I'm getting a 403 error? 知道我为什么会收到403错误吗? As far as I know I'm in the right scope and I have the correct json auth file stored. 据我所知,我在正确的范围内,我已经存储了正确的json auth文件。 I can also do other things like list users with this code and not receive a 403 error. 我还可以使用此代码执行列表用户等其他操作,但不会收到403错误。

Perhaps there's some extra authentication I need to do. 也许我需要做一些额外的身份验证。

Any help would be much appreciated. 任何帮助将非常感激。

Cheers 干杯

I obviously can't read. 我显然无法阅读。

Solved this by deleting credentials from ~/.credentials/ 通过删除〜/ .credentials /中的凭据解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM