private async Task <EbayOAuthToken> RefreshUserToken(long companyId) { try { OAuth2Api oAuth = new OAuth2Api(); var refreshToken = await GetEbayRefreshTokenByCompanyId(companyId); if (refreshToken != null) { var newUserAccessToken = oAuth.GetAccessToken(OAuthEnvironment.PRODUCTION, refreshToken.Token, new List <string> { "https://api.ebay.com/oauth/api_scope/sell.inventory" }); var newUserToken = new EbayOAuthToken { Token = newUserAccessToken.AccessToken.Token, Expiration = newUserAccessToken.AccessToken.ExpiresOn.ToUniversalTime(), Type = EbayOAuthTokenType.USERTOKEN }; SetEbayTokenByCompanyId(companyId, newUserToken).Wait(); return(newUserToken); } throw new Exception("Refresh token was null!"); } catch (Exception ex) { telemetryClient.TrackException(ex); return(null); } }
public async Task <bool> SetEbayTokenByCompanyId(long companyId, EbayOAuthToken token) { try { var secretIdentifier = string.Empty; switch (token.Type) { case EbayOAuthTokenType.USERTOKEN: secretIdentifier = $"ebay-user-token-company{companyId}"; break; case EbayOAuthTokenType.REFRESHTOKEN: secretIdentifier = $"ebay-refresh-token-company{companyId}"; break; default: throw new InvalidTokenTypeException("The token has an invalid type."); } var secretAttributes = new SecretAttributes { Expires = token.Expiration.Value.ToUniversalTime() }; //check to see if identifier exists var versionList = await keyVaultClient.GetSecretVersionsAsync(appSettings.Value.KeyVaultUrl, secretIdentifier); if (!versionList.Any()) { await keyVaultClient.SetSecretAsync(appSettings.Value.KeyVaultUrl, secretIdentifier, token.Token, null, null, secretAttributes); } else //exists, update it { await keyVaultClient.SetSecretAsync(keyVaultUrl, secretIdentifier, token.Token, null, null, secretAttributes); } return(true); } catch (Exception ex) { telemetryClient.TrackException(ex); return(false); } }