【问题标题】:Can't access Analytics from Google Apps Script无法从 Google Apps 脚本访问 Analytics
【发布时间】:2015-01-11 02:49:40
【问题描述】:

我已在 Google Developers Console 中打开了我的 Analytics API,并且我的凭据(客户端 ID 和客户端密码)输入正确。这是我的 Google Apps 脚本中的代码:

var clientId = "(censored)";
var consumerSecret = "(censored)";

var oAuthConfig = UrlFetchApp.addOAuthService('analytics');
oAuthConfig.setAccessTokenUrl("https://accounts.google.com/o/oauth2/token");
oAuthConfig.setRequestTokenUrl("https://accounts.google.com/o/oauth2/token?scope=https://www.googleapis.com/auth/analytics.readonly");
oAuthConfig.setAuthorizationUrl("https://accounts.google.com/o/oauth2/auth");
oAuthConfig.setConsumerKey(clientId);
oAuthConfig.setConsumerSecret(consumerSecret);

var url = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:91669545&start-date=2014-12-01&end-date=2014-12-31&metrics=ga:sessions,ga:bounces";
var response = UrlFetchApp.fetch(url,{"muteHttpExceptions":true});
Logger.log(response);

我的“响应”返回 401 错误,这意味着它甚至没有在 API 上提交授权信息,否则它会返回说我的凭据不正确。 Google Apps 脚本应用程序也向我抛出此错误:

访问未配置。您的项目未启用 API,或者您的 API 密钥上配置了 per-IP 或 per-Referer 限制,并且请求与这些限制不匹配。请使用 Google Developers Console 更新您的配置。

关于为什么我不能让 oauth 在这里工作有什么想法吗?谢谢!

【问题讨论】:

    标签: google-apps-script google-analytics oauth-2.0 google-oauth


    【解决方案1】:

    UrlFetchApp.setOAuthService() 不支持您正在尝试使用的OAuth2

    参考 Google 团队之一对问题跟踪器上的请求的回答。:Update #9 to issue #929

    有 1 或 2 种方法可以从 Apps 脚本建立 OAuth2 连接,但不是这种方式。

    链接的答案中引用的方法:https://github.com/googlesamples/apps-script-oauth2http://ramblings.mcpher.com/Home/excelquirks/oauthtoo/ezyoauth2


    试试 Apps Script 内置的 Analytics API 方法 - 这可能涵盖了您对 Analytics 集成所需的内容:https://developers.google.com/apps-script/advanced/analytics

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多