繁体   English   中英

无法通过Google Apps脚本访问Google 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);

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

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

关于为什么我无法在这里工作的任何想法? 谢谢!

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

引用Google团队之一对问题跟踪器提出的要求的答案。: 更新#9,发布#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM