来自谷歌脚本的融合表

我在谷歌脚本中使用此代码从请求到谷歌融合表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 Build 连接?

回答(1)

2 years ago

不是oAuth 2.0,Apps脚本仅支持1.0 . 但是您可以使用Fusion Tables API正常使用1.0 . 不需要用户传递信息 . 这是我很久以前写的一个函数,用于将oAuth和我的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"};
}

现在还有一个notable library链接在Google Apps脚本网站上,可以自动为您提供更多内容 . 请看一下 .