【问题标题】:Retrieve the organization name from Admin Directory API从 Admin Directory API 检索组织名称
【发布时间】:2019-01-31 10:37:50
【问题描述】:

我正在使用https://flask-oauthlib.readthedocs.io/en/latest/ 处理 Google OAuth API。我能够连接和检索用户数据。但仍然面临获取用户组织名称的问题。

根据文档https://developers.google.com/admin-sdk/directory/v1/guides/manage-users,API 应该返回组织名称。

"organizations": [ { "name": "Google Inc.", "title": "SWE",
“primary”:true,“customType”:“”,“description”:“软件 工程师”}],

但是有

SCOPES_ORGANISATIONS = ("https://www.googleapis.com/auth/admin.directory.user.readonly "
                        "https://www.googleapis.com/auth/admin.directory.orgunit.readonly")

app_ = oauth.remote_app(
    'google',
    consumer_key=GOOGLE_CLIENT_ID,
    consumer_secret=GOOGLE_CLIENT_SECRET,
    request_token_params={'scope': f'email profile {SCOPES_ORGANISATIONS}', 'prompt': 'login'},
    base_url='https://www.googleapis.com/oauth2/v1/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://accounts.google.com/o/oauth2/token',
    authorize_url='https://accounts.google.com/o/oauth2/auth')

GOOGLE_DIRECTORY_API = "https://www.googleapis.com/admin/directory/v1/users/{user_email}"

def get_profile(app_):
    url = GOOGLE_DIRECTORY_API.format(user_email=email)
    ret = app_.get(url)
    logger.info("Google api %s return, %r", url, ret.data)

    name = ret.data["organizations"][0]["name"]
    logger.info("Company name, %r", name)

    return name

返回的结果是这样的

{
 "kind": "admin#directory#user",
 "id": "xxxxxxxxxxxxxxx",
 "etag": "",
 "primaryEmail": "ali@xxxxxxx",
 "name": {
  "givenName": "Ali",
  "familyName": "xxxxx",
  "fullName": "xxxxx"
 },
 "isAdmin": true,
 "isDelegatedAdmin": false,
 "lastLoginTime": "2019-01-31T10:03:27.000Z",
 "creationTime": "2019-01-31T08:19:26.000Z",
 "agreedToTerms": true,
 "suspended": false,
 "archived": false,
 "changePasswordAtNextLogin": false,
 "ipWhitelisted": false,
 "emails": [
  {
   "address": "xxxxx",
   "primary": true
  },
  {
   "address": "xxxxx"
  }
 ],
 "nonEditableAliases": [
  "xxxxxx"
 ],
 "customerId": "xxxxxxxx",
 "orgUnitPath": "/",
 "isMailboxSetup": true,
 "isEnrolledIn2Sv": false,
 "isEnforcedIn2Sv": false,
 "includeInGlobalAddressList": true
}

【问题讨论】:

  • organization 返回用户所属的组织列表。如果您没有得到任何回报,我建议该用户不属于任何组织
  • 使用的是Gsuite admin,也有可能不返回?
  • 很抱歉,我不明白您的最后评论,请您再试一次。我怀疑我们有语言问题。
  • 抱歉,用户是 Gsuite 管理员。难不成,他不属于主组织?

标签: google-api google-workspace google-directory-api flask-oauthlib


【解决方案1】:

Directory API, User organizations- name field. Where does it come from? 指出解决方案。

现在我可以通过以下方式检索组织名称:

GOOGLE_DIRECTORY_API = "https://www.googleapis.com/admin/directory/v1/users/{user_email}"
GOOGLE_CUSTOMER_API = "https://www.googleapis.com/admin/directory/v1/customers/{customer_id}"

def get_profile(app_):
    url = GOOGLE_DIRECTORY_API.format(user_email=email)
    ret = app_.get(url)
    logger.info("Google api %s return, %r", url, ret.data)

    # https://stackoverflow.com/q/39571207/280485
    # the organization data may be in the user data
    if ret.data.get("organizations"):
        name = ret.data.get("organizations")[0].get("name")
        logger.info("Company name, %r", name)
        return name

    # in case of not retrieve organization data from customer (the same use) API
    customer_id = ret.data["customerId"]

    url = GOOGLE_CUSTOMER_API.format(customer_id=customer_id)
    ret = app_.get(url)

    logger.info("Google api %s return, %r", url, ret.data)

    name = ret.data["postalAddress"]["organizationName"]
    logger.info("Company name, %r", name)

    return name

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多