4

我已经使用 Postman 和 Charles 来查看我的 Smartsheet GET 函数是否有效,一切都很好,我得到了数据 json 字符串。

我尝试从本地代码和 Google 应用脚本 html 页面运行调用。

但我从 Google 应用脚本页面收到此错误:

“XMLHttpRequest 无法加载https://api.smartsheet.com/2.0/sheets/ MY SMART SHEET ID。对预检请求的响应未通过访问控制检查:在请求的资源。因此,不允许访问来源“ 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);
});
4

2 回答 2

3

You cannot call the Smartsheet API from client-side JavaScript due to the fact that the API doesn't support CORS at this time.

You can call the Smartsheet API directly from a Google Apps Script. In fact, we/Smartsheet publish two Google Add-ons that both use the Smartsheet API from scripts (1,2).

The Google apps-script-oauth2 project provides a complete example of using the Smartsheet API in their sample directory on GitHub. See samples/Smartsheet.gs.

With the OAuth token out of the way, you can make requests to the Smartsheet API like so:

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);

Note that getSmartsheetService() in the above example is just like getDriveService() in Google's Readme except for Smartsheet. The full code is below:

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)
        ;
}
于 2016-04-12T08:02:17.057 回答
0

在Google Apps Script API下的 external APIs 下,

Google Apps 脚本可以与来自整个网络的 API 进行交互。

连接到公共 API

Apps Script 中提供了数十个 Google API,作为内置服务或高级服务。如果您想使用不能用作 Apps 脚本服务的 Google(或非 Google)API,您可以通过 URL Fetch 服务连接到 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 票,将他在谷歌应用程序脚本中的代码连接到 smartsheet api。

于 2016-04-11T07:08:03.020 回答