0

我想通過Apps腳本試用Google數據存儲,因爲我有一個基於Google工作表的當前解決方案,該解決方案遇到了與Drive文件不斷交易時固有的超時問題。我使用服務帳戶在Google雲中創建了一個測試項目,並啓用了庫MZx5DzNPsYjVyZaR67xXJQai_d-phDA33 (cGoa)來處理Oauth2工作。我按照指南啓動它here並得到了與我的令牌一起工作的所有相關確認(並且刪除令牌會引發'驗證失敗提示')。使用應用腳本URLFetchApp訪問Google數據存儲數據

現在我要開始一個基本的查詢,顯示我已經放在一個實體,我可以使用API​​瀏覽器here並運行此查詢的身體:

{ 
    "query": {} 
} 

,並得到這樣的結果:

{ 
"batch": { 
"entityResultType": "FULL", 
"entityResults": [ 
    { 
    "entity": { 
     "key": { 
     "partitionId": { 
      "projectId": "project-id-5200707333336492774" 
     }, 
     "path": [ 
      { 
      "kind": "Transaction", 
      "id": "5629499534213120" 
      } 
     ] 
     }, 
     "properties": { 
     "CommentIn": { 
      "stringValue": "My First Test Transaction" 
     }, 
     "Status": { 
      "stringValue": "Closed" 
     }, 
     "auditStatus": { 
      "stringValue": "Logged" 
     }, 
     "User": { 
      "stringValue": "John Doe" 
     }, 
     "Start": { 
      "timestampValue": "2017-08-17T18:07:04.681Z" 
     }, 
     "CommentOut": { 
      "stringValue": "Done for today!" 
     }, 
     "End": { 
      "timestampValue": "2017-08-17T20:07:38.058Z" 
     }, 
     "Period": { 
      "stringValue": "08/16/2017-08/31/2017" 
     } 
     } 
    }, 
    "cursor": "CkISPGogc35whh9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyGAsSC1RyYW5zYWN0aW9uGICAgICAgIAKDBgAIAA=", 
    "version": "1503004124243000" 
    } 
], 
"endCursor": "CkISPGogc35wcm9qZWN0LWlkLTUyMDAxxDcwODA1MDY0OTI3NzRyGAsSC1RyYW5zYWN0aW9uGICAgICAgIAKDBgAIAA=", 
"moreResults": "NO_MORE_RESULTS" 
} 
} 

我嘗試做同樣的事情,此代碼:

function doGet(e) 
    { 
    var goa = cGoa.GoaApp.createGoa('Oauth2-Service-Account', 

    PropertiesService.getScriptProperties()).execute(e); 
    if(goa.hasToken()) {var token = goa.getToken();} 

     var payload = {"query":{}} 
      ; 

     var result = UrlFetchApp.fetch('https://datastore.googleapis.com/v1/projects/project-id-5200707333336492774:runQuery', 
        { 
         method: "POST", 
         headers: {authorization: "Bearer " + goa.getToken()}, 
     muteHttpExceptions : true, 
         payload: payload 
        }); 



     Logger.log(result.getBlob().getDataAsString()); 
      } 

,並得到這個電子RROR在記錄器:

"error": { 
    "code": 400, 
    "message": "Invalid JSON payload received. Unknown name \"query\": Cannot bind query parameter. 'query' is a message type. Parameters can only be bound to primitive types.", 
    "status": "INVALID_ARGUMENT", 
    "details": [ 
     { 
     "@type": "type.googleapis.com/google.rpc.BadRequest", 
     "fieldViolations": [ 
      { 
      "description": "Invalid JSON payload received. Unknown name \"query\": Cannot bind query parameter. 'query' is a message type. Parameters can only be bound to primitive types." 
      } 
     ] 
     } 
    ] 
    } 
} 

如果我嘗試用另一個詞,如「資源」或「GqlQuery」,我得到這個錯誤:

"error": { 
    "code": 400, 
    "message": "Invalid JSON payload received. Unknown name \"GqlQuery\": Cannot bind query parameter. Field 'GqlQuery' could not be found in request message.", 
    "status": "INVALID_ARGUMENT", 
    "details": [ 
     { 
     "@type": "type.googleapis.com/google.rpc.BadRequest", 
     "fieldViolations": [ 
      { 
      "description": "Invalid JSON payload received. Unknown name \"GqlQuery\": Cannot bind query parameter. Field 'GqlQuery' could not be found in request message." 
      } 
     ] 
     } 
    ] 
    } 
} 

我無法從API文檔告訴我的語法應該是什麼。任何人都可以告訴我如何編寫一個功能請求體從Apps腳本到數據存儲?

+0

另外:運行一個空的身體查詢創建此迴應:「錯誤」:{ 「代碼」:400, 「消息」:「必須設置Query.query和Query.gql_query字段之一」, 「status」:「INVALID_ARGUMENT」 } } –

回答

1

你需要設置你的有效載荷的的contentType以及字符串化的JSON有效載荷如下:

var result = UrlFetchApp.fetch(
    'https://datastore.googleapis.com/v1/projects/project-id-5200707333336492774:runQuery', 
    { 
     'method':'post', 
     'contentType':'application/json', 
     'headers': {authorization: "Bearer " + goa.getToken()}, 
     'payload':JSON.stringify(payload) 
    } 
); 
+0

您做到了,Dimu Designs!謝謝!它現在很好用!那麼,有沒有這個API的參考,我沒有找到?谷歌的API頁面沒有說清楚... –

+0

通過HTTP發送請求和接收響應對於一般的Web開發來說是普遍的,所以Apps腳本文檔有點掩蓋了它。但它確實涵蓋了UrlFetchApp文檔中的主題:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app –

+0

如果您真的想更深入地研究該主題你可以嘗試解決以下指南: https://code.tutsplus.com/tutorials/http-the-protocol-every-web-developer-must-know-part-1--net-31177 這是很多咀嚼,但你會更好地瞭解一旦你混淆了http的工作原理。 –

相關問題