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

Knoldus Inc.
Knoldus - Technical Insights
2 min readJan 5, 2013

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.

[code language=”html”]
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”};
}
[/code]
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.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com