2016-04-10 58 views
1

我已經使用Postman和Charles來查看我的Smartsheet GET函數是否工作,並且一切正常,我得到了數據json字符串。如何使用Google Apps腳本致電Smartsheet API?

我試着從本地代碼和Google應用程序腳本html頁面運行調用。

但我從谷歌應用程序腳本頁面此錯誤:

「的XMLHttpRequest無法加載https://api.smartsheet.com/2.0/sheets/我的智能表單ID迴應預檢要求未通過訪問控制檢查:沒有「訪問控制 - 允許 - Origin'標題出現在請求的資源上,因此'https://n-n662xy6uqbadudjpoghatx4igmurid667k365ni-script.googleusercontent.com'不允許訪問。「

我的目標是從Smartsheet表單自動更新Google工作表。

我的Ajax請求是這樣的:

var settings = { 
    "async": true, 
    "crossDomain": true, 
    "url": "https://api.smartsheet.com/2.0/sheets/SHEET_ID", 
    "method": "GET", 
    "headers": { 
    "authorization": "Bearer MY_SECRET_ACCESS_TOKEN", 
    "cache-control": "no-cache", 
    "postman-token": "SOME_LONG_TOKEN" 
    } 
} 

$.ajax(settings).done(function (response) { 
    console.log(response); 
}); 

回答

0

下的外部API Google Apps Script API下,

谷歌Apps腳本可以與來自全國各地的網絡API的交互。

連接到公共API

谷歌API的數十名提供Google Apps腳本,無論是內置的服務或高級服務。如果您想要使用不適用於Apps腳本服務的Google(或非Google)API,則可以通過網址抓取服務連接到API的公用HTTP接口。

以下示例向YouTube API發出請求,並返回與查詢skateboarding dog匹配的視頻供稿。

var url = 'https://gdata.youtube.com/feeds/api/videos?' 
+ 'q=skateboarding+dog' 
+ '&start-index=21' 
+ '&max-results=10' 
+ '&v=2'; 
var response = UrlFetchApp.fetch(url); 
Logger.log(response); 

下面是相關SO ticket是連接他的代碼在谷歌Apps腳本來smartsheet API。

1

不能由於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) 
     ; 
} 
+0

我可以使用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); } }' –

+0

你缺少哪些數據?有關如何確保返回所需信息的信息,請參閱http://smartsheet-platform.github.io/api-docs/?shell#get-sheet中getSheet的API文檔的include參數。 –

相關問題