2017-12-27 23 views
0

Google 드라이브 시트를 사용하여 간단한 데이터 저장소로 사용하고 있습니다. Google은 클라이언트 사이드 스크립트 호출 브라우저을 통해 공개 시트에서 피드를 얻는 것이 매우 편리합니다.클라이언트 측 스크립트 호출을 서버로 이동하려면 어떻게합니까? (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='https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxx/od6/public/full?alt=json-in-script&callback=useJSONdata'></script> 
</body> 

이 기술에는 많은 정보가 있습니다. Old technique herehere. handy flash card tool I wrote에 대한 링크가 있습니다. 코드는 모두 브라우저의 개발 도구에 표시됩니다.

현재 사용중인 경우 서버에있는 node.js의 Google 스프레드 시트 데이터에 액세스하고 싶습니다. 유효한 클라이언트 코드에서 <script src='https://...&callback=...'></script>이라는 최상의 번역본을 볼 수 없습니다.

저는 GET과 POST 모두 https 요청을 사용하여이 작업을 시도하고 있습니다. 나는 그 요청이 데이터를 얻고 있음을 알 수 있지만, 반환 된 데이터는 어지러운 것이다. 나는 내용의 10 라인과 스프레드 시트에 반환 된 정보의 7000 라인을 찾고 있어요. 내 노드에서 반환 된 데이터를 찾고 있었어요

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 

: curl https://spreadsheets.google.com/feeds/list/1s9-AyqYqp25s2OeGiglCSNF2UcvJJm50Ipvm8v0Mgo0/od6/public/full?alt=json-in-script

데이터 브라우저에서 <script src=... 요청을 반환 : 여기

내 테스트 스프레드 시트 데이터를보고 컬 명령입니다 .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 

는 Google 드라이브 스프레드 시트의 데이터에 대한 서버 요청에 그 <script src=... 코드를 변환하는 더 좋은 방법이 있나요? 여기에 온 사람 있니?

+0

Google 스프레드 시트를 볼 수있는 전체 node.js 라이브러리는 다음과 같습니다. https://developers.google.com/sheets/api/quickstart/nodejs –

+0

@ChrisRiebschlager 분명히 올바른 방법이 가장 좋습니다. 앞으로. 새로운 Google 스프레드 시트 API (ver 4)는 이전 단순 피드에 필요하지 않은 OAuth 권한을 사용합니다. 앞으로는 새로운 프로젝트를 위해 활용 하겠지만 이전 API 버전 피드에 대한 내 자신의 질문에 대답하려고합니다. 링크에 대한 THX. – zipzit

답변

0

그래서 나는 이것을 알아낼 수있었습니다. 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"); 
    } 
} 

이것은 충분히 쉽게 작동했습니다. 한 가지 트릭이 있었는데 피드 주소에 ?alt=json을 사용했습니다.

참고 :이 기술은 현재 OAuth 토큰/자격 증명이 포함 된 version 4 of the Google Sheets Api,으로 대체 된 것으로 보입니다. 이 API의 사용은 모든 새로운 작업에 대해 의미가 있습니다.