gpt4 book ai didi

c# - 如何解决 google analytics api 调用中的 403 错误

转载 作者:行者123 更新时间:2023-11-30 18:40:22 24 4
gpt4 key购买 nike

我正在使用以下帖子中的代码: Google Analytics API - Programmatically fetch page views in server side

但在下面突出显示的行中出现 403 禁止错误。我不认为这是一个凭据问题,因为我的凭据是正确的,因为我已经检查并仔细检查过,并且我还使用这些凭据登录到分析帐户。所以可能是某种文件夹权限问题?

//-------------- Get Auth Token -------------------

WebClient webClient = new WebClient();
NameValueCollection data = new NameValueCollection();
data.Add("accountType", "GOOGLE");
data.Add("Email", "xxxx@gmail.com");
data.Add("Passwd", "xxxx");//Passwd, not a misspell.
data.Add("service", "analytics");
data.Add("source", "xxxx-xxxx-xx");//Could be anything.

byte[] bytes = webClient.UploadValues("https://www.google.com/accounts/ClientLogin", "POST", data);
string tokens = Encoding.UTF8.GetString(bytes);
string authToken = extractAuthToken(tokens);

//-------------- Get page views -------------------

string feed = "https://www.google.com/analytics/feeds/data";

//Required:
string ids = "ga:xxxx";
string metrics = "ga:pageviews";
string startDate = "2011-06-25";
string endDate = "2011-07-25";

//Optional:
string dimensions = "ga:pagePath";
string sort = "-ga:pageviews";

string feedUrl = string.Format("{0}?ids={1}&dimensions={2}&metrics={3}&sort={4}&start-date={5}&end-date={6}",
feed, ids, dimensions, metrics, sort, startDate, endDate);

webClient.Headers.Add("Authorization", "GoogleLogin " + authToken);

// This is the line I get the 403 error on:
**string result = webClient.DownloadString(feedUrl);**

//-------------- Extract data from xml -------------------

XDocument xml = XDocument.Parse(result);
var ns1 = "{http://www.w3.org/2005/Atom}";
var ns2 = "{http://schemas.google.com/analytics/2009}";

var q = from entry in xml.Descendants()
where entry.Name == ns1 + "entry"
select new
{
PagePath = entry.Element(ns2 + "dimension").Attribute("value").Value,
Views = entry.Element(ns2 + "metric").Attribute("value").Value
};

//-------------- Do something with data -------------------
foreach (var page in q)
{
Debug.WriteLine(page.PagePath + " " + page.Views);
}

//-------------- Help Method -------------------
private string extractAuthToken(string data)
{
var tokens = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
return tokens.Where(token => token.StartsWith("Auth=")).Single();
}

最佳答案

如果您过于频繁地调用 Google Analytics API,you could get 403 Forbidden errors .从该链接:

General Analytics API quotas. These apply to both the Analytics APIs, i.e., Management API and Core Reporting API:
- 50,000 requests per project per day
- 10 queries per second (QPS) per IP

当我的应用程序连续调用太多次时,我发现 AdWords API 返回了 403 错误,这可能是导致您出现问题的原因。

编辑

如果您根本无法调用任何电话,请查看列出的步骤 here在“开始之前”下。根据文档,您需要先通过 Google API 控制台注册您的应用程序,然后才能使用该 API。

关于c# - 如何解决 google analytics api 调用中的 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com