我正在尝试连接到Google Analytics报告API以获取基本的网页浏览统计信息。我试着按照这个教程(http://www.arboundy.com/2012/04/getting-started-with-google-analytics-in-c/)。我无法设置正确的位以获得成功的身份验证,因为谷歌似乎最近更改了API,因此原始配置似乎不起作用。
这是我目前所拥有的:
Service = new AnalyticsService("MyDemoApp");
Service.setUserCredentials("user@gmail.com", "password");
AccountQuery AccountsQuery = new AccountQuery("https://www.googleapis.com/analytics/v3/data/ga"/*Not sure what goes here this gives a 400*/);
AccountFeed AccountsFeed = Service.Query(AccountsQuery); // 400 error here
如何通过V3 api(看起来是我从NuGet获得的那个)连接到这个的任何想法
答案 0 :(得分:3)
这必须适用于你在c#中。 (我已经尝试过并且工作过)
string username = "youremailuser@domain.com";
string pass = "yourpassword";
string gkey = "?key=YourAPIkEY";
string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;
string accountFeedUrl = "https://www.googleapis.com/analytics/v2.4/management/accounts" + gkey;
AnalyticsService service = new AnalyticsService("WebApp");
service.setUserCredentials(username, pass);
DataQuery query1 = new DataQuery(dataFeedUrl);
query1.Ids = "ga:12345678";
query1.Metrics = "ga:visits";
query1.Sort = "ga:visits";
query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
query1.StartIndex = 1;
DataFeed dataFeedVisits = service.Query(query1);
foreach (DataEntry entry in dataFeedVisits.Entries)
{
string st = entry.Title.Text;
string ss = entry.Metrics[0].Value;
visits = ss;
}