最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何将客户端脚本调用移动到服务器? (Google云端硬盘电子表格Feed)

运维笔记admin13浏览0评论

如何将客户端脚本调用移动到服务器? (Google云端硬盘电子表格Feed)

如何将客户端脚本调用移动到服务器? (Google云端硬盘电子表格Feed)

我正在使用Google驱动程序表并将它们用作简单的数据存储。谷歌通过浏览器中的客户端脚本调用从公共工作表中获取提要非常友好。

<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=';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

从浏览器<script src=...请求返回的数据:

encoding:"UTF-8"
feed:
    author:[{…}]
    category:[{…}]
    entry:(167) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
    id:{$t: ""}
    link:(4) [{…}, {…}, {…}, {…}]
    openSearch$startIndex:{$t: "1"}
    openSearch$totalResults:{$t: "167"}
    title:{type: "text", $t: "Sheet1"}
    updated:{$t: "2015-04-04T19:31:32.089Z"}
    xmlns:""
    xmlns$gsx:""
    xmlns$openSearch:".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 Drive Spreadsheet的数据?以前有人来过这里吗?

回答如下:

所以我能够弄清楚这个。我最终使用'request'工具代替HTTPS格式。

var request = require('request');
request.get(
    'https://spreadsheets.google/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对所有新工作都有意义。

发布评论

评论列表(0)

  1. 暂无评论