To get user's profile information in google sc

2019-03-05 09:23发布

I want to get my domain's user's profile information..I am using profile API for this..

Profile API gives only a few fields like (Family Name, Given Name,Email etc)..But i also want to get these fields also: occupation,department,Phone [Work], Phone [Mobile], Relation [Manager]...

i am not able to get these fields..So please give me suggestion to get these fields.

2条回答
干净又极端
2楼-- · 2019-03-05 09:46

There isn't a built in API for that in Apps Script directly, but if you are using a Google Apps for Business or Education, you can use the Google Apps through UrlFetch and oAuth. That should expose any profile information that is stored in Google Apps.

查看更多
在下西门庆
3楼-- · 2019-03-05 10:01

I got my all fields by Profile API.... Actually if any field is empty then Profile API doesn't give that field as an output..... So using Profile API, we can get a user's full information....

Code :
    var username="user "
    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'

    var name=""
    var occupation=""
    var department=""
    var email=""
    var contactNumber_1=""
    var contactNumber_2=""
    var relation=""
    var account=""
    var office=""
    var personal_account=""
    var current_account=""
    var language=""
    var blog=""
    var image=""
    try{
      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].rel.split("#")[1]=='work')
            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
          else if(phonelist[i].rel.split("#")[1]=='mobile')
            contactNumber_2=phonelist[i].$t
        }
      }catch(err){}
      try{   
        relation=profileInfo.gContact$relation[0].$t+" ["+profileInfo.gContact$relation[0].rel+"]"
      }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"};
}

If any field is empty then that tag will not be found thats why all the fields are written in try - catch block...

查看更多
登录 后发表回答