![](/img/trans.png)
[英]HttpWebRequest throws “The remote server returned an error: (404) Not Found.” for 404
[英]The remote server returned an error: (404) Not Found. google implentation in c#
我正在使用 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.googleapis.com/analytics/v3/data/ga?key=api_key&dimensions= ga:city,ga: date&end-date=2016-03-17&ids= ga: 123456789&metrics= ga: visits ,ga:sessions,ga:pageValue,ga:bounces,ga:bounceRate,ga:pageviews&start-date=2016-03-15
不知何故,它重定向到https://www.google.com/accounts/ClientLogin被谷歌关闭。
解决方案:
安装当前版本的 Google .net 客户端库
PM> 安装包 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();
从我的Google Analytis api 教程中撕下的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.