Search code examples
google-oauthgoogle-apps

How can you get a user's OAUTH id from a google apps domain directly?


I'm writing a web application that users log in to via google OAUTH 2.0. When they first log in, I match an email address I have on record with the OAUTH token's email address, but end up storing the google user id value as the important part - this is a long number, which ends up showing up in your google+ profile URL if you don't have a custom URL. Ideally, I could skip that step and pre-populate my user database, since I am only allowing people from a particular google apps domain to log in.

What I'd like is a way to generate a list of mappings like 'user 10110233402123 is bob@example.com' from some element of my google apps domain control structure, instead of having to capture this during the OAUTH login dance. Ideally, this would be from the domain control panel, but looking up users directly in the control panel doesn't seem to expose that particular piece of information.


Solution

  • I do not believe you can get that information from the Control Panel, but you can get it from the Directory API.

    More particularly, if you use the users.list() API call you will retrieve the list of your users (paginated). Each user will have an id field, thats what you're looking for.

    Here's a typical users.list() API response. The id for admin two is 123456789.

    {
     "kind": "directory#users",
     "users": [
      {
       "kind": "directory#user",
       "id": "123456789",
       "username": "admin2@example.com",
       "name": {
        "givenName": "admin",
        "familyName": "two",
        "fullName": "admin two"
       },
       "isAdmin": true,
       ...
     "nextPageToken": "NNNNN"
    }
    

    If you are not into making API calls, the excellent tool GAM made by Ditto allows you to list users in a CSV list, where ID is the column you're looking for.