您不能由於API目前不支持CORS,因此從客戶端JavaScript調用Smartsheet API。
您可以直接從Google Apps腳本調用Smartsheet API。實際上,我們/ Smartsheet發佈了兩個使用腳本中的Smartsheet API(1,2)的Google附加組件。
Google apps-script-oauth2 project提供了在GitHub的示例目錄中使用Smartsheet API的完整示例。見samples/Smartsheet.gs。
使用OAuth令牌的方式進行,你可以發出請求Smartsheet API,像這樣:
var url = 'https://api.smartsheet.com/2.0/users/me';
var options = {
'method': 'get'
, 'headers': {"Authorization": "Bearer " + getSmartsheetService().getAccessToken() }
};
var response = UrlFetchApp.fetch(url, options).getContentText();
Logger.log("email:" + JSON.parse(response).email);
注意getSmartsheetService()
在上面的例子一樣,在谷歌的自述getDriveService()
除了Smartsheet。完整的代碼如下:
function getSmartsheetService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('scott_smartsheet')
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl('https://app.smartsheet.com/b/authorize')
.setTokenUrl('https://api.smartsheet.com/2.0/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId(SMARTSHEET_CLIENT_ID)
.setClientSecret(SMARTSHEET_CLIENT_SECRET)
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope('READ_SHEETS')
// Set the handler for adding Smartsheet's required SHA hash parameter to the payload:
.setTokenPayloadHandler(smartsheetTokenHandler)
;
}
我可以使用Mr.Robot提到的fetchURL方法。但是,響應僅包含我正在訪問的智能表中的有限數據,爲什麼? 「功能callSmartSheet(){ 變種PARAMS = { 「報頭」:{ 「授權」: 「承載」, 「緩存控制」: 「無緩存」, }, 「方法」 :「GET」, } var response = UrlFetchApp.fetch(「https://api.smartsheet.com/2.0/sheets/ /」,params); var responseCode = response.getResponseCode();如果(responseCode === 200){ Logger.log(response); } else { Logger.log(responseCode); } }' –
你缺少哪些數據?有關如何確保返回所需信息的信息,請參閱http://smartsheet-platform.github.io/api-docs/?shell#get-sheet中getSheet的API文檔的include參數。 –