我正在使用OAuth2凭据,收到404错误:
using Google.Analytics;
using Google.GData.Analytics;
void Analytics()
{
try
{
string userName = ConfigurationManager.AppSettings["username"];
string passWord = ConfigurationManager.AppSettings["password"];
string gkey = "key=api _key";
string dataFeedUrl = "https://www.googleapis.com/analytics/v3/data/ga";//+ gkey;
AccountQuery query = new AccountQuery();
AnalyticsService service = new AnalyticsService("Web App");
service.setUserCredentials(userName, passWord);
DataQuery query1 = new DataQuery(dataFeedUrl);
query1.Ids = "ga:123456789";
query1.Metrics = "ga:visits,ga:sessions,ga:pageValue,ga:bounces,ga:bounceRate,ga:pageviews";
query1.Dimensions = "ga:city,ga:date";
query1.GAStartDate = ("2016-03-15");//DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");
query1.GAEndDate = ("2016-03-17");//DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
query1.ExtraParameters = gkey;
DataFeed dataFeedVisits = service.Query(query1);
foreach (DataEntry entry in dataFeedVisits.Entries)
{
string st = entry.Title.Text;
string ss = entry.Metrics[0].Value;
int _intVisists = Int32.Parse(ss);
Response.Write("<br/>");
Response.Write("Total Visits : " + ss);
Response.Write("<br/>");
}
}
catch (Exception ex)
{
Response.Write("Error : " + ex);
}
}
例外是:
以某种方式将其重定向到https://www.google.com/accounts/ClientLogin,谷歌已将其关闭。
答案 0 :(得分:0)
<强>解决方案:强>
安装当前版本的Google .net客户端库
PM&GT;安装包Google.Apis.Analytics.v3
<强>认证强>
string[] scopes = new string[] {AnalyticsService.Scope.AnalyticsReadonly}; // View Google Analytics Data
var clientId = "[Client ID]"; // From https://console.developers.google.com
var clientSecret = "xxx"; // From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
ClientSecret = clientSecret},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;
创建分析服务
var service = new AnalyticsService(new BaseClientService.Initializer() { HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",});
请求数据
DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:8903098", "2014-01-01", "2014-01-01", "ga:sessions");
request.MaxResults = 1000;
GaData result = request.Execute();
中删除