在Google脚本中获取用户的个人资料信息 [英] To get user's profile information in google script

查看:197
本文介绍了在Google脚本中获取用户的个人资料信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Profile API只提供了几个字段,例如(系列名称,给定名称,电子邮件等)..但我也想获得这些领域也:
职业,部门,电话[工作],电话[手机],关系[经理] ... $ / b
$ b

我无法获得这些字段。所以请给我建议以获取这些字段。

解决方案

我通过Profile API获取了所有字段....实际上,如果任何字段为空,那么Profile API不会将该字段作为输出.....
因此,使用Profile API,我们可以获得一个用户的完整信息....

 代码:
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 var contact =
var contact =
var contact =
var department =
var department =
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 {
职业= profileInfo.gContact $职业。$ t
} catch(err){}
try {
department = profileInfo.gd $ organization [0] .gd $ orgDepartment。$ t
} catch(err ){}
尝试{
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){ }
var {phonelist = profileInfo.gd $ phoneNumber
for(var i = 0; i< phonelist.length; i ++){
if(phonelist [i]。 ('#')[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验证
* /

函数googleOAuth_(名称,范围){
var oAuthConfig = UrlFetchAp p.addOAuthService(名称);
oAuthConfig.setRequestTokenUrl(https://www.google.com/accounts/OAuthGetRequestToken?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};





$ b

如果任何字段为空,那么将不会找到该标记这就是为什么所有字段被写入try - catch块...


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.

解决方案

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

这篇关于在Google脚本中获取用户的个人资料信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆