【问题标题】:Google API user creation with service account使用服务帐户创建 Google API 用户
【发布时间】:2021-09-30 12:16:07
【问题描述】:

我正在尝试使用 Googles Directory 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 应该具有正确的权限。有什么建议吗?

【问题讨论】:

  • 对于域范围的委派,您提到您完成了 GCP 部分,那么 admin console 的部分呢?
  • 是的。在管理安全面板上,我在 API 控制下看到了该服务帐户的正确客户端 ID,我还可以看到它可以访问的范围。所以这部分看起来不错,不过是个好建议
  • 该错误表明凭证帐户没有具有足够的权限。您是否有权访问可以进行身份​​验证的管理员帐户而不是服务帐户?这将消除您的代码作为问题并确认服务帐户配置不正确或权限不足。
  • 您的代码没有使用模拟/委托:credentials = credentials.with_subject(USER_ACCOUNT_EMAIL)

标签: google-cloud-platform google-api-python-client


【解决方案1】:

出现此错误有两种可能性。

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

  2. 如果被冒充用户未启用相关服务。

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

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

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

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

这个link 也可能会有所帮助。

【讨论】:

    【解决方案2】:

    感谢您的意见。你们都在一定程度上是正确的。基本上有两个问题。服务帐户用户需要被委派需要域管理员操作的域管理员权限,域范围的委派是不够的。此外,管理控制台中的域范围和代码中的范围定义需要更广泛。有一个 github issue 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()
    

    【讨论】:

      猜你喜欢
      • 2022-11-08
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多