问题描述
我正在构建一个数据库,我希望能够在Fusion Table中托管数据库,并且正在开发一些用于连接Fusion API的测试功能。我正在使用Google Apps脚本,并严重依赖其他来源获取此代码。我花了大部分时间来研究这个问题,现在我陷入了困境。
I'm building a database that I hope to host in a Fusion Table and I'm developing some test functions for interfacing with the Fusion API. I'm using Google Apps Scripts and have relied heavily on other sources for this code. I've spent most of a day researching this problem and I am now stuck.
在脚本编辑器中运行addNewColumn()时,它会到达获取就绪日志条目(正如我在日志中看到的那样),然后它会通过身份验证过程(每次),然后停止(并且永远不会进入获取已执行日志条目)。没有错误抛出,但它永远不会完成读取命令。
When running addNewColumn() in the script editor, it gets to the "fetch ready" log entry (as I can see in the log), it then goes through the authentication process (every time), then just stops (and never gets to the "fetch executed" log entry). There are no errors thrown yet it never completes the fetch command.
在调试模式下运行时,它会通过授权,然后挂在获取线上。如果我点击介入,我会得到一个错误,简单地说OAuth错误。我不确定在这里做什么。任何帮助将不胜感激。
When running in debug mode, it goes through authorization then hangs at the fetch line. If i click 'step-in' I get an error that simply says "OAuth Error". I'm not really sure what to do here. Any help would be much appreciated.
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);
}
Upate
我简化了我的函数以进行测试,并且我发现只需在有效负载字段中插入'null'来代替'payload'变量代码将在融合表中成功创建一个无名列。但是,当我重新插入有效载荷变量 - 它停止工作。
I've simplified my functions to test and I've discovered that by simply inserting 'null' in place of the 'payload' variable in the payload field of the options body, the code will successfully create a nameless column in the fusion table. However, when I re-insert the payload variable - it stops working.
不工作时,它要么:每次从编辑器运行它时要求重新授权,或当从Web应用程序运行状态遇到错误:需要授权才能执行该操作。有效载荷如何更改授权?我已经将完全相同的有效载荷剪切并粘贴到OAuth2.0操场中,并且完美地工作。请帮助。
When not working, it either: asks to reauthorize every time I run it from the editor, or when run from a web app states "Error Encountered: Authorization is required to perform that action". How does the payload change authorization? I've cut and pasted the exact same payload into the OAuth2.0 playground and it works perfectly. Please help.
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为'application'或'json',它将运行,但仍然不会按预期通过有效内容。
I have found a way that lets it run without any errors but it still does not assign the requested name and type to the new column. The most recent addition was specifying the content type as shown below. By forcing the contentType to 'application' or 'json', it will run but still doesn't pass the payload as intended.
var options =
{
"oAuthUseToken":"always",
"oAuthServiceName":service,
"method" : "POST",
"payload" : payload,
'contentType' : 'application'
};
推荐答案
已解决!解决方案有两种:
SOLVED!!! The solution was twofold:
- 必须通过JSON.stringify()将有效内容转换为字符串
- contentType必须手动设置为application / json。
这与我的多个教程非常不同在线找到但想分享我的工作解决方案。
This is very different than the multiple tutorials I've found online but wanted to share my working solution.
function addColumn(authToken) {
googleAuth();
var payload =
{
name : 'YES',
type : 'STRING'
};
var options =
{
payload : JSON.stringify(payload),
oAuthUseToken : "always",
oAuthServiceName : service,
method : "POST",
contentType : "application/json"
};
var url = fetchUrl + "tables/" + fusionId + "/columns";// + "?key=" + apiKey;
var fetchResult = UrlFetchApp.fetch(url, options);
Logger.log(fetchResult.getHeaders());
Logger.log(fetchResult.getContentText());
return Utilities.jsonParse(fetchResult.getContentText());
}
这篇关于获取Google Apps脚本以授权Fusion Table API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!