处理Google Apps脚本生成的json时发生颤振异常错误
开发我的第一款Flatter移动应用程序,这是一段代码片段,用于从中获取json:处理Google Apps脚本生成的json时发生颤振异常错误,json,flutter,google-apps-script,google-sheets,Json,Flutter,Google Apps Script,Google Sheets,开发我的第一款Flatter移动应用程序,这是一段代码片段,用于从中获取json: 'https://my-json-server.typicode.com/typicode/demo/posts' …成功响应、解码、解析等。然后,当我使用上载到git的文档进行测试时: 'https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json' ……这也行得通。但当我尝试使用自己的数据时(Google Web Apps脚
'https://my-json-server.typicode.com/typicode/demo/posts'
…成功响应、解码、解析等。然后,当我使用上载到git的文档进行测试时:
'https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json'
……这也行得通。但当我尝试使用自己的数据时(Google Web Apps脚本将Google Sheets电子表格发布为json):
…我得到错误:
Exception has occurred.
FormatException (FormatException: Unexpected character (at line 2, character 1)
<!DOCTYPE html>
^
)
发生异常。
FormatException(FormatException:意外字符(第2行,字符1)
^
)
以下是我的颤振片段:
import 'dart:convert';
import 'package:theirmenu001pt00/tm_menuitem_model.dart';
import 'package:http/http.dart';
class HttpService {
// final String postsUrl = "https://my-json-server.typicode.com/typicode/demo/posts";
// final String postsUrl = "https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json";
final String postsUrl = "https://script.googleusercontent.com/macros/echo?user_content_key=7zmRRkd__iPae6VZ9oq5TTNjfEm3QQV9EYBvQN-awvPS4-HNw2C4wbUSC8ud0J9rfFuxXvwhWPMjiJj5GUVQvGHDvinAYraCm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnPKmEGJr49ifP_3P8Fcnrtzcwn0zyFgFMfS_we8kf_vIvupeaUN7ec2K60MRzRqUBQ&lib=MNDmyszRDOPMr7WJ3Tg4jKCcl7uh4ZtSK";
Future<List<Post>> getMenuItems() async {
Response res = await get(postsUrl);
if (res.statusCode == 200) {
List<dynamic> body = jsonDecode(res.body);
List<Post> posts =
body.map((dynamic item) => Post.fromJson(item)).toList();
return posts;
} else {
throw "Can't get posts.";
}
}
}
导入'dart:convert';
导入“package:theirmenu001pt00/tm_menuitem_model.dart”;
导入“包:http/http.dart”;
类HttpService{
//最后一个字符串postsUrl=”https://my-json-server.typicode.com/typicode/demo/posts";
//最后一个字符串postsUrl=”https://raw.githubusercontent.com/rays-github/theirmenu/master/db.json";
最后一个字符串postsUrl=”https://script.googleusercontent.com/macros/echo?user_content_key=7zmRRkd__iPae6VZ9oq5TTNjfEm3QQV9EYBvQN-awvPS4-HNW2C4WBUSC8UD0J9RFFUXXWHWPMJJ5UVQVGHDVINAYRACM5\u BXDLH2JW0NU2ODEMN9CCS20OX\u 1XSNCGQAQJX\u RYFHECZENPKMEGJR49IFP\u 3P8FCNRTZYCWN0ZYFGFMFs\u we8kf\u vivoupeaun7EC2K60MRZRQUBQ&lib=MNDMYSZRDOPMR7WJG4JKCCL7UH4ZTSK”;
未来的getMenuItems()异步{
Response res=等待获取(postsUrl);
如果(res.statusCode==200){
列表主体=jsonDecode(res.body);
列出职位=
body.map((动态项)=>Post.fromJson(项)).toList();
返回岗位;
}否则{
抛出“无法获取帖子”;
}
}
}
以下是我的Google Web应用程序脚本:
function doGet(e){
// Sheet url
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1jsBS-RBNRxYU66WFkJHvrzHLGmNqxBzzQfaHJO6i6UY/edit#gid=446843772");
// Sheet Name
var sheet = ss.getSheetByName("Users");
return getUsers(sheet);
}
function getUsers(sheet){
var jo = {};
var dataArray = [];
// collecting data from 2nd Row , 1st column to last row and last column
var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
for(var i = 0, l= rows.length; i<l ; i++){
var dataRow = rows[i];
var record = {};
record['userId'] = dataRow[0];
record['id'] = dataRow[1];
record['title'] = dataRow[2];
record['body'] = dataRow[3];
dataArray.push(record);
}
jo = dataArray;
var result = JSON.stringify(jo);
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
函数doGet(e){
//工作表url
var ss=SpreadsheetApp.openByUrl(“https://docs.google.com/spreadsheets/d/1jsBS-RBNRxYU66WFkJHvrzHLGmNqxBzzQfaHJO6i6UY/edit#gid=446843772");
//图纸名称
var sheet=ss.getSheetByName(“用户”);
返回用户(张);
}
函数getUsers(表){
var jo={};
var dataArray=[];
//从第二行、第一列到最后一行和最后一列收集数据
var rows=sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
对于(var i=0,l=rows.length;我最肯定的是,您会看到一个登录屏幕,而不是JSON,因为您没有向应用程序脚本端点发出经过身份验证的请求。您没有提到错误在flatter代码中的何处抛出,但我猜它发生在jsonDecode
语句中。请尝试注销放进去,你会看到页面的其余部分。我认为你使用的URL就像https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###
是的Web应用程序的重定向URLhttps://script.google.com/macros/s/###/exec
。但是当您在没有重定向的情况下请求该URL时,302的状态为返回。在这种情况下,需要使用重定向请求https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###
。首先,您可以使用一个简单的curl命令进行测试,以确认是否返回了您期望的值吗?在本例中,它是curl-L"https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###“
。当可以检索到正确的值时,如何修改脚本以重定向并再次测试它?啊,是的。看来你们两个(@tehhowch,@Tanaike)已经确定了错误的有效原因。作为新手,我仍在尝试解决这些问题和可能的解决方案。但在浏览器中导航到该url之所以有效,是因为我是以web应用程序脚本的创建者身份登录的。因此,当我的Flatter应用程序运行时(我猜是以非用户身份运行的)返回一个访问请求页面。那么,@tehhowch,在请求之前可以让我的颤振应用程序进行身份验证吗?…这是一个安全的解决方案吗..让一个公共可用的移动应用程序向我的google web apps脚本发出经过身份验证的请求?如果不是,我的公共可用颤振应用程序是否有合适和/或安全的方式运行my google web apps脚本?几乎可以肯定,您会看到一个登录屏幕而不是JSON,因为您没有向您的应用程序脚本端点发出经过身份验证的请求。您没有提到在颤振代码中抛出错误的位置,但我猜它发生在jsonDecode
语句中。请尝试记录输出您将看到页面的其余部分。我认为您使用的URL类似于https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###
是的Web应用程序的重定向URLhttps://script.google.com/macros/s/###/exec。但是当您在没有重定向的情况下请求该URL时,302的状态为returned。在这种情况下,需要使用重定向来请求https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###
。首先,您可以使用一个简单的curl命令进行测试,以确认是否返回了您期望的值吗?在本例中,它是curl-L“https://script.googleusercontent.com/macros/echo?user_content_key=###&lib=###“
。当可以检索到正确的值时,如何修改脚本以重定向并再次测试它?啊,是的。看来你们两个(@tehhowch,@Tanaike)已经确定了错误的有效原因。作为新手,我仍在尝试解决这些问题和可能的解决方案。但在浏览器中导航到该url之所以有效,是因为我是以web应用程序脚本的创建者身份登录的。因此,当我的Flatter应用程序运行时(我猜是以非用户身份运行的)返回一个访问请求页面。那么,@tehhowch,在请求之前可以让我的颤振应用程序进行身份验证吗?…这是一个安全的解决方案吗..让一个公共可用的移动应用程序向我的google web apps脚本发出经过身份验证的请求?如果不是,我的公共可用颤振应用程序是否有合适和/或安全的方式运行m谷歌网络应用脚本?
function doGet(e){
// Sheet url
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1jsBS-RBNRxYU66WFkJHvrzHLGmNqxBzzQfaHJO6i6UY/edit#gid=446843772");
// Sheet Name
var sheet = ss.getSheetByName("Users");
return getUsers(sheet);
}
function getUsers(sheet){
var jo = {};
var dataArray = [];
// collecting data from 2nd Row , 1st column to last row and last column
var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
for(var i = 0, l= rows.length; i<l ; i++){
var dataRow = rows[i];
var record = {};
record['userId'] = dataRow[0];
record['id'] = dataRow[1];
record['title'] = dataRow[2];
record['body'] = dataRow[3];
dataArray.push(record);
}
jo = dataArray;
var result = JSON.stringify(jo);
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}