Получить название организации из Admin Directory API - PullRequest
0 голосов
/ 31 января 2019

Я использую https://flask -oauthlib.readthedocs.io / en / latest / для работы с Google OAuth API.Я могу подключиться и получить пользовательские данные.Но все еще сталкивается с проблемой, чтобы получить имя организации пользователя.

Согласно документации https://developers.google.com/admin-sdk/directory/v1/guides/manage-users, API должен возвращать название организации.

"organization": [{"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
}

1 Ответ

0 голосов
/ 01 февраля 2019

Каталог API, Пользовательские организации - поле имени.Откуда это взялось? Укажите мне на решение.

Теперь я могу получить название организации по:

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
...