如何获得谷歌个人资料信息,包括自定义字段从一个应用程序域用户



使用用户。配置文件和用户。email范围和/oauth2/v2/userinfo提要似乎没有返回任何自定义字段(在我的例子中是Department)或电话号码。这些字段显示在域共享联系人目录中。

是否可能有一个Apps Domain特定的feed URL,如/oauth2/{Domain}/v2/userinfo ?

API/Service是否还不支持任何自定义字段?

是否有办法使其工作?

阅读访问你自己的应用程序域共享联系人配置文件,连接到你的帐户应该不是那么困难。

我更喜欢非管理解决方案,因为我的域使用通用访问卡w/SAML身份验证,所以我不能只是存储管理员凭据(用户:密码)在一个应用程序引擎应用程序和访问/m8/feed。如果使用事先授权的消费者密钥和秘密来访问域共享联系人(带有自定义字段)的流程,我会对如何使其工作的说明感兴趣。

EDIT Jay Lee搞定了"https://www.google.com/m8/feeds/gal/{domain}/full"

这是使用Google Apps script的概念证明脚本(当我完成它时,我会添加最终的OAuth2版本)

function getGal(email, passwd, domain) {
  var res = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
    contentType: "application/x-www-form-urlencoded",
    method: "post",
    payload: { "Email": email, "Passwd": passwd, "accountType": "HOSTED", "service":"cp" }
  });
  var auth = res.getContentText().match(/Auth=(.*)/i)[1];
  Logger.log("Auth: " + auth);
  res = UrlFetchApp.fetch("https://www.google.com/m8/feeds/gal/" + domain + "/full", {
    method: "get",
    headers: { "Authorization": "GoogleLogin auth=" + auth, "GData-Version": "1.0" }
  });
  Logger.log(res.getHeaders());
  Logger.log(res.getContentText());
}

EDIT 2 OAuth版本,返回JSON和仅为用户访问脚本的信息。

function googleOAuthM8() {
  var oAuthConfig = UrlFetchApp.addOAuthService("m8");
  oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope=https://www.google.com/m8/feeds/');
  oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken');
  oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
  oAuthConfig.setConsumerKey('anonymous');
  oAuthConfig.setConsumerSecret('anonymous');
  return {oAuthServiceName:"m8", oAuthUseToken:'always'};
}
function getGal(domain) {
  res = UrlFetchApp.fetch("https://www.google.com/m8/feeds/gal/" + domain + "/full?alt=json&q=" + Session.getActiveUser().getEmail(), googleOAuthM8());
  Logger.log(res.getHeaders());
  Logger.log(res.getContentText());
}

任何非管理员用户都可以通过编程方式访问GAL,参见:

https://github.com/google/gfw-deployments/blob/master/apps/shell/gal/gal_feed.sh

我不相信这个API调用是文档化的或官方支持的,但它甚至可以与OAuth身份验证一起工作,而不是示例的ClientLogin(在OAuth 2.0 playground上使用非admin用户和标准https://www.google.com/m8/feeds/ Contacts作用域进行测试)。

请注意,全局地址列表是用户配置文件,组和共享联系人的汇编。您需要对其进行解析,以查找希望获取部门信息的用户。

我会利用Google Apps Profiles API来做到这一点。它会给你一堆元信息,包括个人资料数据,甚至个人资料照片:https://developers.google.com/google-apps/profiles/

即使您正在使用PIV/CAC/SAML,您也可以使用双足认证。https://developers.google.com/accounts/docs/OAuth GoogleAppsOAuth

两条腿的oauth是阻力最小的路径,但是您也应该看看OAuth2,特别是jwt签名的服务帐户部分——然而,使用旧的GData xml api可能有点棘手。

就可用的字段而言,您将不得不使用本页上的字段。还有一些扩展属性,你可以在其中添加任意数据,但它们不会显示在谷歌邮件本身的联系人浏览器中:https://developers.google.com/gdata/docs/2.0/elements gdProfileKind

顺便说一句,如果你在LDAP环境中(既然你提到了CAC,我想你可能是),你应该看看Google Apps Directory Sync,它可以同步配置文件数据与本地AD/LDAP。

来源:我将Google Apps部署到大型组织(3000+),公共和私人。

我使用了以下方法与TwoLeggedOAuthHmacToken:用户密钥和秘密可以在google apps管理仪表板

中找到。
CONSUMER_KEY = 'domain.com'
CONSUMER_SECRET = 'secret_key'

class ContactClient():
    def __init__(self, username):
        # Contacts Data API Example ====================================================
        self.requestor_id = username + '@' + CONSUMER_KEY
        self.two_legged_oauth_token = gdata.gauth.TwoLeggedOAuthHmacToken(
            CONSUMER_KEY, CONSUMER_SECRET, self.requestor_id)
        self.contacts_client = gdata.contacts.client.ContactsClient(source=SOURCE_APP_NAME)
        self.contacts_client.auth_token = self.two_legged_oauth_token
    def newuser(self, username):
        self.contacts_client.auth_token.requestor_id = username + '@' + CONSUMER_KEY
    def getContacts(self, username=None):
        if username:
            self.newuser(username)
        return self.contacts_client.GetContacts()

class MainPage(webapp2.RequestHandler):
    def get(self):
        contacts = ContactClient(username='username')
        feed = contacts.getContacts()
        output = ""
        if feed:
              for entry in feed.entry:
                if entry.title and entry.title.text:
                    output += entry.title.text + "<br/>"
                for email in entry.email:
                    if email.primary and email.primary == 'true':
                        output += '&nbsp;%s<br/>' % (email.address)
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write('''<h1>Contact Access via GData Client</h1>''' + output)

最新更新