invalid credential error
AccountFeed accountFeed =
service.Query(query);
代码:
public partial class _Default : System.Web.UI.Page
{
Int32 _intVisists = 0;
Int32 _intPageViewVisit = 0;
protected void Page_Load(object sender, EventArgs e)
{
RefreshFeed();
}
private void RefreshFeed()
{
string userName = "xyz@gmail.com";
string passWord = "xyz";
string gkey = "AIzaSfafaG_JUzUqkKsPd0-mPBZO4GrDF1hbbbb";
string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;
AccountQuery query = new AccountQuery();
AnalyticsService service = new AnalyticsService(AnalyticsService.GAnalyticsService);
if (!string.IsNullOrEmpty(userName))
{
service.setUserCredentials(userName, passWord);
}
string str = "";
AccountFeed accountFeed = service.Query(query);
foreach (AccountEntry entry in accountFeed.Entries)
{
str = entry.ProfileId.Value;
}
DataQuery query1 = new DataQuery(dataFeedUrl);
query1.Ids = str;
query1.Metrics = "ga:visits,ga:bounces";
query1.Sort = "ga:visits";
query1.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
query1.StartIndex = 1;
int cnt = 0;
double incntDone = 0.0;
DataFeed dataFeed11 = service.Query(query1);
foreach (DataEntry entry in dataFeed11.Entries)
{
string st = entry.Title.Text;
string ss = entry.Metrics[0].Value;
string _str = DisplayPercentage(double.Parse(entry.Metrics[1].Value) / int.Parse(entry.Metrics[0].Value));
double incnt = double.Parse(entry.Metrics[1].Value) / double.Parse(entry.Metrics[0].Value);
incntDone = Math.Round(incnt * 100, 2);
cnt = cnt + int.Parse(ss);
}
Response.Write("Bounce Rate : " + incntDone + "<br/>");
query1.Ids = str;
query1.Metrics = "ga:pageviews";
query1.Sort = "ga:pageviews";
query1.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
DataFeed dataFeedpageviews = service.Query(query1);
foreach (DataEntry entry in dataFeedpageviews.Entries)
{
string st = entry.Title.Text;
string ss = entry.Metrics[0].Value;
_intPageViewVisit = Int32.Parse(ss);
Response.Write("<br/>");
Response.Write("Total PageView : " + ss);
Response.Write("<br/>");
}
query1.Ids = str;
query1.Metrics = "ga:visits";//visitors
query1.Sort = "ga:visits";
query1.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.AddDays(-3).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;
_intVisists = Int32.Parse(ss);
Response.Write("<br/>");
Response.Write("Total Visits : " + ss);
Response.Write("<br/>");
}
答案 0 :(得分:2)
UserCredential
登录示例:
/// <summary>
/// Authenticate to Google Using Oauth2
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
/// <param name="userName">A string used to identify a user.</param>
/// <returns></returns>
public static AnalyticsService AuthenticateOauth(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { AnalyticsService.Scope.Analytics, // view and manage your analytics data
AnalyticsService.Scope.AnalyticsEdit, // edit management actives
AnalyticsService.Scope.AnalyticsManageUsers, // manage users
AnalyticsService.Scope.AnalyticsReadonly}; // View analytics data
try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
中删除