【问题标题】:Cannot change includeInGlobalAddressList using Directory API无法使用 Directory API 更改 includeInGlobalAddressList
【发布时间】:2014-12-03 08:37:19
【问题描述】:

我正在尝试将脚本从 Profiles API 迁移到 Directory API。其目的是使用 includeInGlobalAddressList 属性管理某些 Google Apps 域用户的联系信息的可见性。

我可以获取用户对象,并且可以提交补丁请求,但没有进行更改。从 patch 方法返回的对象具有 includeInGlobalAddressList 的原始值。在 api 文档中有一些对 etag 的模糊引用,但我不清楚 etag 是否应该包含在补丁正文中或作为 If-Match 标头。 (如果答案是 If-Match 标头,那么我应该如何使用 python 库传递它?)

http = httplib2.Http()
http = credentials.authorize(http)

service = build("admin", "directory_v1", http=http)
usersvc=service.users()
d=usersvc.get(userKey=userkey, projection="basic",fields="etag,id,includeInGlobalAddressList").execute()
visible = d['includeInGlobalAddressList']
# logic to decide what change elided. this is "transition from visible to invisible contact"
pch=json.dumps({'includeInGlobalAddressList': False, 'etag': d['etag']})
print pch
res=usersvc.patch(userKey=userkey, body=pch, fields="etag,id,includeInGlobalAddressList").execute()
print json.dumps(res, indent=4)

我得到的结果是:

{"includeInGlobalAddressList": false, "etag": "\"WIg4sZOp0a-9Z5MJXVMQx1SQW5A/avXX6NaPX78Y6qFG7S4TqKFMIEU\""}
{
    "includeInGlobalAddressList": true, 
    "etag": "\"WIg4sZOp0a-9Z5MJXVMQx1SQW5A/J0MuBtIvJoTcifknCMScMlyCQnc\"", 
    "id": "109793672165131484748"
}

即使我不使用 fields 或传递 etag,也会发生这种情况。

【问题讨论】:

  • 我认为您不应该在将补丁正文传递给 patch() 方法之前将其转换为字符串。

标签: google-admin-sdk google-api-python-client google-directory-api


【解决方案1】:

在 PATCH 请求之前无需执行 GET。 PATCH 的想法是仅通过线路发送更改的数据。试试:

service = build("admin", "directory_v1", http=http)
usersvc = service.users()
res = usersvc.patch(userKey=userkey, body={"includeInGlobalAddressList": False}, fields="id,includeInGlobalAddressList").execute()
print res

【讨论】:

  • 虽然如果事情不这样的话,这将节省往返行程,但 PATCH 方法似乎对此属性没有任何影响这一事实无济于事。 (如果解决方案的一部分是包含 etag,我将需要 GET 来读取 etag)
  • 我看到你的示例代码删除了错误的 json.dumps() 原来是问题。但如果不是上面 Eric 的评论,我不会注意到它
猜你喜欢
  • 2013-07-27
  • 1970-01-01
  • 2013-06-06
  • 2014-10-21
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
相关资源
最近更新 更多