微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用服务帐户创建 Google API 用户

如何解决使用服务帐户创建 Google API 用户

我正在尝试使用 Google 目录 API 和服务帐户创建用户。但是我收到错误

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://admin.googleapis.com/admin/directory/v1/users?alt=json returned "Not Authorized to access this resource/api". Details: "Not Authorized to access this resource/api">

我在 Google 控制台上创建了一个服务帐户并允许域范围内的委派。它还说我的项目启用了 Admin SDK API。但是我似乎无法创建用户。文档让我有点困惑。这是我的实现

def create_googleuser(content,randpass):
    ''' This function creates a Google Apps account for a user passing webhook contents and password as arguments '''
    
    # Get User info from Webhook and store them in variables
    firstname = get_firstname(content)
    secondname = get_secondname(content)
    emailaddress = firstname + "." + secondname + "@example.com"
    
    # Connect to Google Api
    userscope = ['https://www.googleapis.com/auth/admin.directory.user']
    service_account_credentials = ('serviceaccountcredentials.json')
    credentials = service_account.Credentials.from_service_account_file(service_account_credentials,scopes=userscope)
    
    userservice = googleapiclient.discovery.build('admin','directory_v1',credentials=credentials)


    # Create a user dictionary with user details
    userinfo = {"primaryEmail": emailaddress,"name":{"givenname":firstname,"familyName":secondname},"password":randpass}
    print (emailaddress)

    # Create user through googleAPI    
    userservice.users().insert(body = userinfo).execute()

我认为我的实现是错误的,而不是权限错误,因为 serviceaccountcredentials.json 应该具有正确的权限。有什么建议吗?

解决方法

出现此错误有两种可能。

  1. 如果 API 方法需要使用模拟用户。

  2. 如果被模拟的用户没有启用相关服务。

案例1的解决方案:
按照 documentation 模拟用户帐户。

情况2的解决方案:
在管理控制台中,打开用户信息并检查该用户是否未被暂停。

打开“应用”面板并检查相关服务是否“开启”。

可能是由于用户没有允许访问该服务的许可(Cloud Identity 而不是 Google Workspace),或者用户所在的单位部门已停用该服务。

这个 link 也可能有帮助。

,

感谢您的投入。你们都说对了一点。基本上有两个问题。服务帐户用户需要被委派需要域管理员操作的域管理员权限,域范围的委派是不够的。此外,管理控制台中的域范围和代码中的范围定义需要更广泛。有 github 问题 open 对这里有帮助:

https://github.com/googleapis/google-api-nodejs-client/issues/1884

我的工作代码如下

def create_googleuser(content,randpass):
    ''' This function creates a Google Apps account for a user passing webhook contents and password as arguments '''
    
    # Get User info from Webhook and store them in variables
    username = get_username(content)
    firstname = get_firstname(content)
    secondname = get_secondname(content)
    emailaddress = firstname + "." + secondname + "@example.com"
    
    # Connect to google API
    userscope = ['https://www.googleapis.com/auth/admin.directory.user','https://www.googleapis.com/auth/admin.directory.user.security']
    service_account_credentials = ('serviceaccountcredentials.json')

    credentials = service_account.Credentials.from_service_account_file(service_account_credentials,scopes=userscope)
    delegated_credentials = credentials.with_subject('domain.admin@example.com')

    userservice = googleapiclient.discovery.build('admin','directory_v1',credentials=delegated_credentials)


    # Create a user dictionary with user details
    userinfo = {"primaryEmail": emailaddress,"name":{"givenName":firstname,"familyName":secondname},"password":randpass}

    # Create user through googleAPI
    userservice.users().insert(body = userinfo).execute()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?