为 Google Admin SDK 创建和设置自定义属性
Posted
技术标签:
【中文标题】为 Google Admin SDK 创建和设置自定义属性【英文标题】:Creating and setting custom attributes for Google Admin SDK 【发布时间】:2019-01-11 19:32:52 【问题描述】:我一直在阅读this documentation,了解如何为用户更新自定义属性。从它的写法来看,我似乎可以做到以下几点:
email = "a@a.com"
results = service.users().list(domain="a.com",projection="full",query='email=0'.format(email)).execute()
if len(results["users"]) == 1:
user = results["users"][0]
user["customSchemas"]["TEST"] = "TEST"
try:
userResponse = service.users().update(userKey=email, body=user).execute()
except HttpError, e:
print(e)
但是,我抛出了错误:
https://www.googleapis.com/admin/directory/v1/users/test%40test.com?alt=json 返回“未授权访问此资源/api”>
我不确定该错误是否是因为我试图错误地更新字段,如果转义 url 中的 @
导致问题,或者我没有正确的范围(我是使用https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.domain, https://www.googleapis.com/auth/admin.directory.userschema
)。
如何创建自定义属性(适用于所有人)并为使用 python SDK 的用户更新它们?
【问题讨论】:
【参考方案1】:这是一个更准确的指南:https://developers.google.com/admin-sdk/directory/v1/guides/delegation#instantiate_an_admin_sdk_directory_service_object
【讨论】:
该链接是通用的 Admin SDK 使用,而不是您在原始问题中提出的操作。【参考方案2】:我建议你参考以下文档:
Create a Custom Schema Set Custom Schema fields for a given user您可以在下面找到如何创建自定义架构并将其用于用户的示例
# third parties imports
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
# modify according to your requirements
CLIENT_SECRET = 'client.json' # the credentials downloaded from the GCP Console
ADMIN_USER = 'admin@yourdomain.com' # The admin user used by the service account
SCHEMA_USER = 'user@yourdomain.com' # The user for which the custom schema will be set
SCHEMA_NAME = 'TestSchema' # the name of the schema we want to work with
# Scopes
SCOPES = ['https://www.googleapis.com/auth/admin.directory.userschema', # to create the schema
'https://www.googleapis.com/auth/admin.directory.user', ] # to manage users
# service account initialization
credentials = ServiceAccountCredentials.from_json_keyfile_name(CLIENT_SECRET, scopes=SCOPES)
delegated_admin = credentials.create_delegated(ADMIN_USER)
admin_http_auth = delegated_admin.authorize(Http())
admin_sdk = build('admin', 'directory_v1', http=admin_http_auth) # Admin SDK service
# we list all the schemas...
schema_list_params =
'customerId': 'my_customer',
'fields': 'schemas/schemaName',
schema_list = admin_sdk.schemas().list(**schema_list_params).execute()
# ... And we create a set with the names of the custom schemas
unique_schemas = set()
for schema in schema_list['schemas']:
unique_schemas.add(schema['schemaName'])
# if the schema we want to work with is not there, we create it
if SCHEMA_NAME not in unique_schemas:
schema_insert_params =
'customerId': 'my_customer',
'body':
'schemaName': SCHEMA_NAME,
'displayName': 'Test Schema',
'fields': [
'fieldName': 'TestField',
'fieldType': 'STRING',
'displayName': 'Test Field',
'multiValued': False,
]
,
schema_insert = admin_sdk.schemas().insert(**schema_insert_params).execute()
# we set a value for our custom schema on the desired user
user_patch_params =
'userKey': SCHEMA_USER,
'body':
'customSchemas':
SCHEMA_NAME:
'TestField': 'My cool test value!'
,
,
,
user_patch = admin_sdk.users().patch(**user_patch_params).execute()
【讨论】:
以上是关于为 Google Admin SDK 创建和设置自定义属性的主要内容,如果未能解决你的问题,请参考以下文章
“调用者没有权限”尝试使用 Firebase Admin SDK 创建自定义令牌
无法使用 Google Directory API Admin SDK 列出用户