将Google Apps配置文件API与Apps脚本一起使用

时间:2013-03-29 06:02:48

标签: google-apps-script

我正在尝试使用Google Apps Profiles Data API检索我域中Google Apps用户的个人资料信息。这里是我们迄今为止尝试过的代码,但它给出了错误Request failed for returned code 403. Server response: Version 1.0 is not supported. (line 7, file "Profile")

function getAllProfiles() {
  var scope = 'https://www.google.com/m8/feeds/profiles';
  var fetchArgs = googleOAuth_('Profile', scope);
  fetchArgs.method = 'GET';
  var domain = UserManager.getDomain();
  var url = scope+'/domain/'+domain+'/full';
  var rawData = UrlFetchApp.fetch(url, fetchArgs).getContentText();
  Logger.log(rawData);
}

//google oAuth
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("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

注意:

  • 我的帐户在Google Apps中具有超级管理员权限
  • 我正在使用Google企业版企业版试用此代码

参考文献: Google Apps Profile Data API

如果有人能指出我正确的方向,那就太棒了

1 个答案:

答案 0 :(得分:3)

以下是修改后的代码,它需要带有请求URL的版本参数。现在代码工作正常。

function getAllProfiles() {
  var scope = 'https://www.google.com/m8/feeds/profiles';
  var fetchArgs = googleOAuth_('Profile', scope);
  fetchArgs.method = 'GET';
  var domain = UserManager.getDomain();
  var url = scope+'/domain/'+domain+'/full?v=3';
  var rawData = UrlFetchApp.fetch(url, fetchArgs).getContentText();
  Logger.log(rawData);
}

//google oAuth
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("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}