无法从 Google Apps 脚本访问 Analytics

Can't access Analytics from Google Apps Script

我已经在 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);

我的 "response" return 是一个 401 错误,这意味着它甚至没有在 API 提交授权信息,否则它会 return 说我的凭据不正确. Google Apps 脚本应用程序也向我抛出此错误:

Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration.

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

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 脚本内置的 Analytics API 方法 - 这可能涵盖您对 Analytics 集成的需求:https://developers.google.com/apps-script/advanced/analytics