我正在尝试创建一个小的python脚本,该脚本将从文件读取并暂停所有读取的用户。我已经使用quickstart了,但是当我尝试使用users().update()
方法并执行来自它的httpRequest时,它给了我403错误。
这是我一直在使用的代码:
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
import sys
# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
def main():
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
# Reading from the text file
print('Getting list of names from names.txt')
f = open("names.txt", 'r')
if f.mode != 'r':
print('Could not read from names.txt')
sys.exit()
email = f.readline()
line = f.readline()
while line:
print('Reading in name: ' + line + '@' + email)
#user = service.users().update()
request = service.users().update(userKey=line + '@' + email, body='{suspended: true}').execute()
#print('User: ' + line + 'Suspended status is: ' + user.get('suspended'))
print(request)
line = f.readline()
if __name__ == '__main__':
main()
这是输出到终端的结果:
Getting list of names from names.txt
Reading in name: *This is a real Email*
Traceback (most recent call last):
File "mass_suspention.py", line 39, in <module>
main()
File "mass_suspention.py", line 33, in main
request = service.users().update(userKey=line + '@' + email, body='{suspended: true}').execute()
File "/Library/Python/2.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users/RealEmail%0A?alt=json returned "Not Authorized to access this resource/api">
我进入了admin.google.com控制台并允许api访问,并确保域api能够正常工作。运行脚本时,我以超级管理员身份登录,并具有该超级管理员拥有的项目中的凭据。我正在测试的帐户是一个没有连接任何组的普通用户的测试帐户。似乎所有权限都已正确设置,但不允许这样做并返回403错误。