Search code examples
javascriptgoogle-apigoogle-admin-sdkgoogle-api-javascript-client

How can I impersonate an admin user from Google API in Javascript?


I am trying to make a request for gapi.client.directory.users.list with the following parameters:

'customer': 'my_customer',
'maxResults': 100,
'orderBy': 'email'

getting a success result when my account is an admin of the domain. When I do the same request using a non-admin account, it return a 403 result.

I went to https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients and added the _______.apps.googleusercontent.com as client and https://www.googleapis.com/auth/admin.directory.user.readonly as scope. Even this way I got a 403 result.

I saw some 'impersonating' functions in Java and Python... but nothing for Javascript in a browser context, not NodeJS.

Can someone help me to allow the request to list users in admin directory api for common users of my domain?


Solution

  • After some browsing, the parameter viewType is required to allow non-admin users to fetch directory data.

    gapi.client.directory.users.list({
        'viewType': 'domain_public',
        'customer': 'my_customer',
        'maxResults': 100,
        'orderBy': 'email'
    }
    

    I found it in the docs: https://developers.google.com/admin-sdk/directory/v1/reference/users/list#domain_public after @jay-lee answer: Non-admin read-only access to Google Admin SDK