0

我在谷歌脚本中使用此代码从请求到谷歌融合表 https://developers.google.com/fusiontables/docs/samples/apps_script

function getGAauthenticationToken(email, password) {
  password = encodeURIComponent(password);
  var response = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
      method: "post",
      payload: "accountType=GOOGLE&Email=" + email + "&Passwd=" + password + "&service=fusiontables&Source=testing"
  });
  var responseStr = response.getContentText();
  responseStr = responseStr.slice(responseStr.search("Auth=") + 5, responseStr.length);
  responseStr = responseStr.replace(/\n/g, "");
  return responseStr;
}

function getdata(authToken) {
  query = encodeURIComponent("SHOW TABLES");
  var URL = "http://www.google.com/fusiontables/api/query?sql=" + query;
  var response = UrlFetchApp.fetch(URL, {
     method: "get",
     headers: {
          "Authorization": "GoogleLogin auth=" + authToken,
     }
  });
  return response.getContentText();
}

function test(){
  var email = "xyz@gmail.com";
  var pass = "xyz";
  var token = getGAauthenticationToken(email,pass);
  Logger.log(getdata(token));
}

但是,我如何在没有用户/密码的情况下与 OAuth 2.0 建立连接?

4

1 回答 1

0

不是 oAuth 2.0,Apps 脚本仅支持 1.0。但是您可以通过 Fusion Tables API 正常使用 1.0。不需要用户通行证信息。这是我很久以前编写的一个函数,用于将 oAuth om 我的 UrlFetch 请求设置为 Google API。

/**
 * Set up Google's oAuth authentication for UrlFetch
 * @retuns {Object} args to be used UrlFetchApp
 * @param {string} name oAuth service name, can be anything
 *     but must not repeat between different scopes
 *     recommend to use a meaninful name related to the scope
 * @param {string} scope google scope for 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 脚本网站上,它会自动为您提供更多的东西。请看一下。

于 2012-07-12T04:52:08.400 回答