This commit is contained in:
2019-10-03 00:12:29 +00:00
parent f482c27cd7
commit 4be88f3b08

View File

@@ -378,15 +378,13 @@ namespace AyaNova.PlugIn.QBOI
/// </summary> /// </summary>
public static void RefreshTokens() public static void RefreshTokens()
{ {
DateTime dtExpireAfter=DateTime.Now.AddMinutes( Q2_TOKEN_LIFESPAN_MINUTES); TimeSpan age = DateTime.Now.Subtract(TOKEN_BIRTHDAY);
if (age.TotalMinutes < Q2_TOKEN_LIFESPAN_MINUTES)
if (TOKEN_BIRTHDAY < dtExpireAfter)
{ {
return; return;
} }
//refresh token here //refresh token here
var v = oac.RefreshTokenAsync(REFRESH_TOKEN).Result; var v = oac.RefreshTokenAsync(REFRESH_TOKEN).Result;
TOKEN_BIRTHDAY = DateTime.Now; TOKEN_BIRTHDAY = DateTime.Now;
ACCESS_TOKEN = v.AccessToken; ACCESS_TOKEN = v.AccessToken;
@@ -447,10 +445,8 @@ namespace AyaNova.PlugIn.QBOI
ACCESS_TOKEN = j["access_token"].Value<string>(); ACCESS_TOKEN = j["access_token"].Value<string>();
REFRESH_TOKEN = j["refresh_token"].Value<string>(); REFRESH_TOKEN = j["refresh_token"].Value<string>();
REALM_ID = j["realmId"].Value<string>(); REALM_ID = j["realmId"].Value<string>();
///***************************************** TESTING FIX THIS LATER *********************** //start the clock on the tokens
TOKEN_BIRTHDAY = DateTime.Now.AddMinutes(-90);//TESTING TESTING TESTING TOKEN_BIRTHDAY = DateTime.Now;
//NOTE: this app will handle refreshing the token in-session as apparently that's possible from here, maybe should test that or could simply just refresh via our server
oac = new OAuth2Client(Q2_CLIENT_ID, Q2_CLIENT_SECRET, Q2_REDIRECT_URL, Q2_ENVIRONMENT); oac = new OAuth2Client(Q2_CLIENT_ID, Q2_CLIENT_SECRET, Q2_REDIRECT_URL, Q2_ENVIRONMENT);
@@ -458,46 +454,34 @@ namespace AyaNova.PlugIn.QBOI
//https://help.developer.intuit.com/s/question/0D50f00005gFeqpCAC/how-do-i-suddenly-get-there-was-an-error-while-communicating-with-the-ids-server //https://help.developer.intuit.com/s/question/0D50f00005gFeqpCAC/how-do-i-suddenly-get-there-was-an-error-while-communicating-with-the-ids-server
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; //Add this just to be sure to use TLS1.2 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; //Add this just to be sure to use TLS1.2
Intuit.Ipp.Security.OAuth2RequestValidator validator = new Intuit.Ipp.Security.OAuth2RequestValidator(ACCESS_TOKEN); Intuit.Ipp.Security.OAuth2RequestValidator validator = new Intuit.Ipp.Security.OAuth2RequestValidator(ACCESS_TOKEN);
SC = new Intuit.Ipp.Core.ServiceContext(REALM_ID, Intuit.Ipp.Core.IntuitServicesType.QBO, validator); SC = new Intuit.Ipp.Core.ServiceContext(REALM_ID, Intuit.Ipp.Core.IntuitServicesType.QBO, validator);
SC.IppConfiguration.Logger.RequestLog.EnableRequestResponseLogging = true; //SC.IppConfiguration.Logger.RequestLog.EnableRequestResponseLogging = true;
// SC.IppConfiguration.Logger.RequestLog.ServiceRequestLoggingLocation = "c:\\temp\\qblogs\\";
SC.IppConfiguration.Logger.RequestLog.ServiceRequestLoggingLocation = "c:\\temp\\qblogs\\";
//' Common setting for both ServiceContexts: //' Common setting for both ServiceContexts:
SC.IppConfiguration.MinorVersion.Qbo = "23"; SC.IppConfiguration.MinorVersion.Qbo = "23";
TODO: variableize this: SC.IppConfiguration.BaseUrl.Qbo = "https://sandbox-quickbooks.api.intuit.com/";
SC.IppConfiguration.BaseUrl.Qbo = "https://sandbox-quickbooks.api.intuit.com/";
//' Serialization Format Json or xml //' Serialization Format Json or xml
SC.IppConfiguration.Message.Request.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json; SC.IppConfiguration.Message.Request.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json;
SC.IppConfiguration.Message.Response.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json; SC.IppConfiguration.Message.Response.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json;
//' Compression Format can be GZip or Deflate: //' Compression Format can be GZip or Deflate:
SC.IppConfiguration.Message.Request.CompressionFormat = Intuit.Ipp.Core.Configuration.CompressionFormat.GZip; SC.IppConfiguration.Message.Request.CompressionFormat = Intuit.Ipp.Core.Configuration.CompressionFormat.GZip;
SC.IppConfiguration.Message.Response.CompressionFormat = Intuit.Ipp.Core.Configuration.CompressionFormat.GZip; SC.IppConfiguration.Message.Response.CompressionFormat = Intuit.Ipp.Core.Configuration.CompressionFormat.GZip;
//' Retry 5 times at an interval of 1 Second if service fails. Note that RetryPolicy moved from Intuit.Ipp.Retry to Intuit.Ipp.Core. //' Retry 5 times at an interval of 1 Second if service fails. Note that RetryPolicy moved from Intuit.Ipp.Retry to Intuit.Ipp.Core.
TimeSpan retryInterval = new TimeSpan(0, 0, 0, 1); TimeSpan retryInterval = new TimeSpan(0, 0, 0, 1);
SC.IppConfiguration.RetryPolicy = new Intuit.Ipp.Core.IntuitRetryPolicy(5, retryInterval); SC.IppConfiguration.RetryPolicy = new Intuit.Ipp.Core.IntuitRetryPolicy(5, retryInterval);
//' For data updates, create DataService: //' For data updates, create DataService:
// Intuit.Ipp.DataService.DataService loDataService = new Intuit.Ipp.DataService.DataService(SC);
Intuit.Ipp.DataService.DataService loDataService = new Intuit.Ipp.DataService.DataService(SC);
_AuthenticationCompleted = true; _AuthenticationCompleted = true;
return true; return true;
@@ -598,6 +582,7 @@ namespace AyaNova.PlugIn.QBOI
} }
} }
todo: call this from all over the place (i.e. before every api call) //and remove logging
RefreshTokens(); RefreshTokens();
IntegrationLog.Log(QBID, "PFC: Authentication completed, validating company data"); IntegrationLog.Log(QBID, "PFC: Authentication completed, validating company data");