Getting user's name from email in Google Apps

2019-08-26 12:09发布

问题:

I have an email from a Google Apps domain. I can see a list of all users in a domain by going into google apps' Contacts page, under 'Domain'.

If I wanted to get their full name from that email, how would I go about doing so? I've tried using ContactsApp to no avail. I also can't use UserManager in the domain tools as I don't have access to the admin tools.

Any ideas on how I could go about this?

回答1:

Your question is similar to Google Script How do I getGivenName() of getActiveUser(), although in that case an admin was trying to create a service that a script run by members of the domain could use to get their own full names.

It's this answer that might help. As a domain user, you can get the full name of people in your own contact list. Add enough people (or the right people), and your success rate will be pretty high.

Here's a modified version of that script.

/**
 * Get a user's name, by accessing contacts.
 *
 * @returns {String} FullName, or UserID
 *                   if record not found in contacts.
 */
function getUserName(email){
  var user = ContactsApp.getContact(email);

  // If user in contacts, return their name
  if (user) {
    // Get full name
    var name = user.getFullName();
  }
  if (!name) {
    // If not in Contacts, return the bald userID.
    name = email.split('@')[0];
  }
  return name;
}


回答2:

Ask the admins to get yourself granted read only access to the provisioning API for users. That and the GAL feed are covered in the answers to this question: How can I get read-only access to the Google Apps Profiles API?