我实施了一项Windows服务,可以下载多个DFA帐户的DoubleClick报告数据。 Windows服务在同一服务器上安装两次,但服务将下载的报告导入到不同的SQL服务器(dev和live)。这两种服务在特定时间每天运行但不平行。 Windows服务使用DFA Reporting API 1.3。以及Google API Client Library 1.7(测试版)。
重新启动Windows服务后的第一天,所有数据都将加载,没有任何错误,但接下来的几天我收到“身份验证令牌已过期”错误。这是代码
public ImportData(string loginUserName, string sourceName)
{
Logger.Info(string.Format("Import for Source {0} started.", sourceName));
this.dbContext = new DatabaseContext();
// Create new log item
this.importExportLog = this.dbContext.ImportExportLogs.Create();
this.importExportLog.SetData("DoubleClick " + sourceName, DateTime.Now);
this.dbContext.ImportExportLogs.Add(this.importExportLog);
this.dbContext.SaveChanges();
this.StartDate = DateTime.Today.AddDays(0 - DoubleClickImporterSettings.Default.ImportDataForDays);
this.EndDate = DateTime.Today;
this.userName = loginUserName;
this.sourceName = sourceName;
// Reset report data
campaignObjs = new List<Campaign>();
clientObjs = new List<Advertiser>();
siteObjs = new List<DfaSite>();
adObjs = new List<AdBase>();
this.user = new DfaUser();
ClientSecrets secret = new ClientSecrets();
secret.ClientId = this.user.Config.OAuth2ClientId;
secret.ClientSecret = this.user.Config.OAuth2ClientSecret;
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secret, new string[] { DfaReportingScope }, this.user.Config.OAuth2PrnEmail, CancellationToken.None, new FileDataStore(DoubleClickImporterSettings.Default.OAuthStorePath)).Result;
// Keep auth toke for 6 hours
credential.Token.ExpiresInSeconds = 21600;
// Create the dfa reporting service.
DfareportingService dfars = new DfareportingService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Double Click Importer",
});
// log in for dfa api
if ((user.Config as DfaAppConfig).AuthorizationMethod == DfaAuthorizationMethod.OAuth2)
{
// Set the OAuth2 scope.
user.Config.OAuth2Scope = DfaReportingScope;
// Since we are using a console application, set the callback url to null.
user.Config.OAuth2RedirectUri = null;
}
else
{
throw new Exception("Authorization mode is not OAuth2.");
}
// Set the username. This is required for the LoginService to work
// correctly when authenticating using OAuth2.
(user.Config as DfaAppConfig).DfaUserName = userName;
this.user.OAuthProvider.RefreshAccessToken();
startDateTime = DateTime.Now;
ImportReportData(dfars);
this.user.OAuthProvider.RefreshAccessToken();
while (!ImportClientData(user))
{
System.Threading.Thread.Sleep(waitUntilNextTry);
this.user.OAuthProvider.RefreshAccessToken();
}
this.user.OAuthProvider.RefreshAccessToken();
while (!ImportAdvertiserData(user))
{
System.Threading.Thread.Sleep(waitUntilNextTry);
this.user.OAuthProvider.RefreshAccessToken();
}
// Import other data...
}
为什么错误不会在第一天发生,而是在其他日期发生?