Google Apps Script : Getting profile information for users in a Google Apps domain


The Profiles Data API allows client applications to retrieve and update profile information for users in a Google Apps domain. Each user profile is stored in the owning user’s Google Account, but it can be modified only by an administrator of the user’s domain.

So what we are going to do, we will fetch user’s profile information from an admin account. For this, we will write a script at admin account of google Apps Domain. Firstly this script will do authorization via Oauth Authentication.

var consumerKey="yourDomain.com";
var consumerSecret="domainSecretkey";
var domainName='yourDomain.com';

function userFullInfo(userEmail)
// user's username
var username=userEmail.split("@")[0]

// Initialize variables
var name=""
var occupation=""
var department=""
var email=""
var contactNumber_1=""
var contactNumber_2=""
var relation=""
var account=""
var office=""

// Oauth Authorization
var base ='https://www.google.com/m8/feeds/'
var fetchArgs = googleOAuth_('contacts', base)
fetchArgs.method='GET'
var url=base+'profiles/domain/'+domainName+'/full/'+username+'?v=3&alt=json'

try{
// Hitting URL to fetch user's profile information
var urlFetch = UrlFetchApp.fetch(url, fetchArgs)
var json=Utilities.jsonParse(urlFetch.getContentText())
var profileInfo = json.entry
try{
name=profileInfo.gd$name.gd$fullName.$t
}catch(err){}

try{
occupation=profileInfo.gContact$occupation.$t
}catch(err){}

try{
department=profileInfo.gd$organization[0].gd$orgDepartment.$t
}catch(err){}

try{
var emaillist=profileInfo.gd$email
for(var i=0;i<emaillist.length;i++){
if(emaillist[i].primary=='true')
email=profileInfo.gd$email[i].address
}
}catch(err){}

try{
var phonelist=profileInfo.gd$phoneNumber
for(var i=0;i<phonelist.length;i++){
if(phonelist[i].rel.split("#")[1]=='work')
contactNumber_1=phonelist[i].$t // phone [work]
else if(phonelist[i].rel.split("#")[1]=='mobile')
contactNumber_2=phonelist[i].$t // Phone [mobile]
}
}catch(err){}

try{
// Account and Office are userdefined field for Google Account. It can be anything
var userDefinedList=profileInfo.gContact$userDefinedField
for(var i=0;i<userDefinedList.length;i++){
  if(userDefinedList[i].key=='Account')
    account=userDefinedList[i].value
  else if(userDefinedList[i].key=='Office')
    office=userDefinedList[i].value
}
}catch(err){}

try{
relation=profileInfo.gContact$relation[0].$t+" ["+profileInfo.gContact$relation[0].rel+"]"
}catch(err){}

try{
var photoURL=profileInfo.link[0].href
var imageContent=UrlFetchApp.fetch(photoURL,fetchArgs).getContent()
// This imageContent varibale contains binary content of photo. So for fetching photo , just store that photo either at Google Cloud Storage or Google drive as public and get URL for that Photo. then we can access that Photo by URL
}catch(err){}
}catch(err){}
}

// Oauth Authentication
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey(consumerKey);
oAuthConfig.setConsumerSecret(consumerSecret);
return {oAuthServiceName:name, oAuthUseToken:"always"};
}

Here all the fields are in try..catch block because if any field entry is empty then that field will not be shown in output and when we will try to fetch that field it will be shown as undefined.

About Rishi Khandelwal

Sr. Software Engineer having more than 5 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Lift Web, Spark, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Cloud, Web and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s