我正在使用Google驱动程序表并将其用作简单的数据存储。 Google通过浏览器中的客户端端脚本调用,从公共工作表中获取Feed非常友好。
<script type="text/javascript">
function useJSONdata(root) {
var feed = root.feed;
var entries = feed.entry || [];
var html = [''];
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
(process the data, etc...)
}
}
</script>
<body>
<script src='https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxx/od6/public/full?alt=json-in-script&callback=useJSONdata'></script>
</body>
有很多关于这种技术的信息。 Old technique here和here。这是指向handy flash card tool I wrote的链接,代码在浏览器的开发工具中都可见。
但在我目前的使用案例中,我希望从服务器中的node.js访问Google电子表格数据。我没有看到<script src='https://...&callback=...'></script>
从文档客户端代码到有效服务器代码的最佳转换。
我一直试图使用https请求(GET和POST)来完成这项工作。我可以看到这些请求正在获取数据,但返回的数据很奇怪。我在一个包含十行内容的电子表格中查看了7000行返回的信息。
这是一个查看我的测试电子表格数据的curl命令:
curl https://spreadsheets.google.com/feeds/list/1s9-AyqYqp25s2OeGiglCSNF2UcvJJm50Ipvm8v0Mgo0/od6/public/full?alt=json-in-script
从浏览器<script src=...
请求返回的数据:
encoding:"UTF-8"
feed:
author:[{…}]
category:[{…}]
entry:(167) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
id:{$t: "https://spreadsheets.google.com/feeds/list/xxxxxxxx/od6/public/full"}
link:(4) [{…}, {…}, {…}, {…}]
openSearch$startIndex:{$t: "1"}
openSearch$totalResults:{$t: "167"}
title:{type: "text", $t: "Sheet1"}
updated:{$t: "2015-04-04T19:31:32.089Z"}
xmlns:"http://www.w3.org/2005/Atom"
xmlns$gsx:"http://schemas.google.com/spreadsheets/2006/extended"
xmlns$openSearch:"http://a9.com/-/spec/opensearchrss/1.0/"
__proto__:Object
我一直在查看我的node.js https GET服务器请求(通过chrome debugger tools)返回的数据,并且该数据无法使用。 POST请求给我一个statusCode 405错误。
response: IncomingMessage
client:TLSSocket {_tlsOptions: {…}, _secureEstablished: true, _securePending: false, _newSessionPending: false, _controlReleased: true, …}
complete:false
connection:TLSSocket {_tlsOptions: {…}, _secureEstablished: true, _securePending: false, _newSessionPending: false, _controlReleased: true, …}
domain:null
headers:{content-type: "application/atom+xml; charset=UTF-8", x-robots-tag: "noindex, nofollow, nosnippet", expires: "Wed, 27 Dec 2017 02:50:15 GMT", date: "Wed, 27 Dec 2017 02:50:15 GMT", cache-control: "private, max-age=0, must-revalidate, no-transform", …}
httpVersion:"1.1"
httpVersionMajor:1
httpVersionMinor:1
method:null
rawHeaders:(38) ["Content-Type", "application/atom+xml; charset=UTF-8", "X-Robots-Tag", "noindex, nofollow, nosnippet", "Expires", "Wed, 27 Dec 2017 02:50:15 GMT", "Date", "Wed, 27 Dec 2017 02:50:15 GMT", "Cache-Control", "private, max-age=0, must-revalidate, no-transform", "Vary", "Accept, X-GData-Authorization, GData-Version", "GData-Version", "1.0", "Last-Modified", "Tue, 26 Dec 2017 20:36:41 GMT", "Transfer-Encoding", "chunked", "P3P", "CP="This is not a P3P policy! See g.co/p3phelp for more info."", "P3P", "CP="This is not a P3P policy! See g.co/p3phelp for more info."", "X-Content-Type-Options", "nosniff", "X-Frame-Options", "SAMEORIGIN", "X-XSS-Protection", "1; mode=block", "Server", "GSE", "Set-Cookie", "NID=120=CFx_iglHazKNNR7tH33m4UkGABss7TV9bB03EVnZrH…=/;Expires=Thu, 28-Jun-2018 02:50:15 GMT;HttpOnly", "Set-Cookie", "NID=120=W3n0zQ1UWJz6pmTOjKjAxuMtG5WOLx2D3OiQca5R9A…=/;Expires=Thu, 28-Jun-2018 02:50:15 GMT;HttpOnly", "Alt-Svc", "hq=":443"; ma=2592000; quic=51303431; quic=5130333…03335,quic=":443"; ma=2592000; v="41,39,38,37,35"", "Connection", "close"]
rawTrailers:[]
read:ƒ (n)
readable:true
req:ClientRequest {domain: null, _events: {…}, _eventsCount: 2, _maxListeners: undefined, output: Array(0), …}
socket:TLSSocket {_tlsOptions: {…}, _secureEstablished: true, _securePending: false, _newSessionPending: false, _controlReleased: true, …}
statusCode:200
statusMessage:"OK"
trailers:{}
upgrade:false
url:""
_consuming:true
_dumped:false
_events:{end: ƒ, data: ƒ}
_eventsCount:2
_maxListeners:undefined
_readableState:ReadableState {objectMode: false, highWaterMark: 16384, buffer: BufferList, length: 0, pipes: null, …}
destroyed:(...)
readableHighWaterMark:(...)
__proto__:Readable
有没有更好的方法将<script src=...
代码转换为来自Google云端硬盘电子表格的数据的服务器请求?有人来过这里吗?
答案 0 :(得分:0)
所以我能够弄清楚这个。我最终使用了&#39;请求&#39;工具,代替HTTPS格式。
var request = require('request');
request.get(
'https://spreadsheets.google.com/feeds/list/1s9-AyqYqp25s2OeGiglCSNF2UcvJJm50Ipvm8v0Mgo0/od6/public/full?alt=json',
function(error, response, body) {
console.log('error: ', error); // print the error if one occurred.
console.log('statusCode: ', response && response.statusCode, "\n\n\n"); // Print the response status code if a response was received
console.log("response.body: ", response.body, "\n\n\n");
}
}
这很容易。有一个技巧,在Feed地址中使用?alt=json
。
请注意,此技术似乎已被version 4 of the Google Sheets Api,取代,现在包含OAuth令牌/凭据。使用该API对所有新工作都有意义。