我建设,我希望能在一个融合表来承载一个数据库,我正在开发一些测试功能与融合API接口。 我使用谷歌Apps脚本,并严重依赖于其他来源的代码。 我大部分时间都在一天研究这个问题,现在我卡住了。
当脚本编辑器运行addNewColumn(),它得到了“获取就绪”日志项(因为我可以在日志中看到),它再通过认证过程(每次),然后就停止变(和永远不会获取在“魂执行”日志条目)。 有没有引发的错误但它从来没有完成取指令。
在调试模式下运行时,它会通过授权,然后在取线挂起。 如果我点击“一步式”我得到的只是说“OAuth错误”的错误。 我真的不知道这里做什么。 任何帮助将非常感激。
function googleAuth() {
var scope = "https://www.googleapis.com/auth/fusiontables";
var service = 'fusion';
var oAuthConfig = UrlFetchApp.addOAuthService(service);
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('{consumer key}');
oAuthConfig.setConsumerSecret('{secret}');
return {oAuthServiceName:'fusion', oAuthUseToken:"always"};
}
function addNewColumn(){
var p = {
'name' : "newColumn",
'type' : "NUMBER"
};
Logger.log(addColumn(p));
}
function addColumn(parameters) {
var url = "https://www.googleapis.com/fusiontables/v1/";
var fetchArgs = googleAuth();
fetchArgs.method = "POST";
fetchArgs.payload = parameters;
//if the fetch arg payload is set to null it will add an unnamed column
//fetchArgs.payload = null;
url += "tables/"+"{table id}"+"/columns";
url += '?key='+"{key}";
Logger.log("fetch ready");
var result = UrlFetchApp.fetch(url, fetchArgs).getContentText();
Logger.log("fetch executed");
return Utilities.jsonParse(result);
}
理解
我已经简化我的功能测试,我已经发现,通过在期权体的有效域简单地插入“空”来代替“有效载荷”变量,代码将成功地创建在融合表中的无名列。 然而,当我重新插入有效载荷变量 - 它停止工作。
不工作的时候,它要么:要求重新授权每次我从编辑器运行时,或者从一个Web应用程序的状态运行时“遇到错误:需要授权才能执行该操作。” 如何有效载荷变化的授权? 我剪切并粘贴完全相同的有效载荷送入了OAuth2.0的操场上和它完美的作品。 请帮忙。
function googleAuth() {
var oAuthConfig = UrlFetchApp.addOAuthService(service);
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(clientId);
oAuthConfig.setConsumerSecret(clientSecret);
return {oAuthServiceName:service, oAuthUseToken:"always"};
}
function addColumn() {
googleAuth();
var payload =
{
name : "IT WORKED",
type : "STRING"
};
var options =
{
"oAuthUseToken":"always",
"oAuthServiceName":service,
"method" : "post",
"payload":payload
};
var url = fetchUrl + "tables/" + fusionId + "/columns" + "?key=" + apiKey;
var fetchResult = UrlFetchApp.fetch(url, options);
Logger.log(fetchResult.getContentText());
return Utilities.jsonParse(fetchResult.getContentText());
}
更新#2
我已经找到一种方法,让它运行没有任何错误,但它仍然不分配请求的名称和类型的新列。 最近除了被指定内容类型,如下所示。 通过强制的contentType为“应用程序”或“JSON”,它将运行,但仍然没有通过有效载荷预期。
var options =
{
"oAuthUseToken":"always",
"oAuthServiceName":service,
"method" : "POST",
"payload" : payload,
'contentType' : 'application'
};