To get user's profile information in google script

前端 未结 2 1026
暖寄归人
暖寄归人 2021-01-26 08:16

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)..B

相关标签:
2条回答
  • 2021-01-26 08:32

    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...

    0 讨论(0)
  • 2021-01-26 08:49

    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.

    0 讨论(0)
提交回复
热议问题