【问题标题】:HttpError 403 : "Not Authorized to access this resource/api"> when trying to add members to a groupHttpError 403:尝试将成员添加到组时“未授权访问此资源/api”>
【发布时间】:2019-04-16 04:23:59
【问题描述】:

这是我的代码:

def addUsersToGroup(users,group):#users is a list of emails.
SCOPES1 = 'https://www.googleapis.com/auth/admin.directory.group'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES1)
    creds = tools.run_flow(flow, store)
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
groupbody={'email':group}
add_group = service.groups().insert(body=groupbody).execute() #making the group

for user in users:
    emailbody = {'email':user}
    group_add=service.members().insert(groupKey=groupbody, body=emailbody).execute()

运行此程序时,我可以创建一个组,但不能向其中添加成员。我已经为我的谷歌管理员帐户启用了域范围的访问范围 https://www.googleapis.com/auth/admin.directory.group

这是错误代码:

raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requestinghttps://www.googleapis.com/admin/directory/v1/groups/%7B%27email%27%3A%20% 
27alfabetet%40ntnui.no%27%7D/members?alt=json returned "Not Authorized 
to access this resource/api">

帮助? :)

【问题讨论】:

    标签: google-admin-sdk


    【解决方案1】:

    根据这个thread,这可能是权限问题。

    似乎这个问题与这里的这个主题有关: Received error "Not Authorized to access this resource/api" when trying to use Google Directory API and Service Account Authentication

    您需要将具有管理员权限的帐户电子邮件传递到凭据中 像这样:

    credentials = SignedJwtAssertionCredentials('CLIENT_EMAIL', 
        key, scope='https://www.googleapis.com/auth/admin.directory.user', sub='superadmin@domain.com')
    

    这个过程现在记录在:https://developers.google.com/admin-sdk/directory/v1/guides/delegation

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2016-12-24
      • 2017-03-16
      • 2021-03-29
      • 2020-06-01
      • 2018-08-01
      • 2014-12-12
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多