/// <summary> /// Obtains credentials asynchronously. Override this method in a concrete builder type if more /// credential mechanisms are supported. /// </summary> protected async virtual Task <IConfigurableHttpClientInitializer> GetHttpClientInitializerAsync(CancellationToken cancellationToken) { if (Credential != null) { return(Credential); } GoogleCredential unscoped = CredentialsPath != null ? await GoogleCredential.FromFileAsync(CredentialsPath, cancellationToken).ConfigureAwait(false) : JsonCredentials != null?GoogleCredential.FromJson(JsonCredentials) : null; // Use default credentials (maybe - see below) // While we accept any credentials that are specified even if there's an API key, // we don't try to load default credentials. if (ApiKey != null && unscoped is null) { return(QuotaProject is null ? null : new ExtraHeadersInitializer( new AccessTokenWithHeaders.Builder { QuotaProject = QuotaProject }.Build(null))); } GoogleCredential scoped = await GetScopedCredentialProvider().GetCredentialsAsync(unscoped, cancellationToken).ConfigureAwait(false); return(QuotaProject is null ? scoped : scoped.CreateWithQuotaProject(QuotaProject)); }
/// <summary> /// Obtains an OIDC token for authentication an IAP request. /// </summary> /// <param name="iapClientId">The client ID observed on /// https://console.cloud.google.com/apis/credentials. </param> /// <param name="credentialsFilePath">Path to the credentials .json file /// downloaded from https://console.cloud.google.com/apis/credentials. /// </param> /// <param name="cancellationToken">The token to propagate operation cancel notifications.</param> /// <returns>The HTTP response message.</returns> public async Task <OidcToken> GetOidcTokenAsync(string iapClientId, string credentialsFilePath, CancellationToken cancellationToken) { // Read credentials from the credentials .json file. GoogleCredential credential = await GoogleCredential .FromFileAsync(credentialsFilePath, cancellationToken).ConfigureAwait(false); // Request an OIDC token for the Cloud IAP-secured client ID. return(await credential .GetOidcTokenAsync(OidcTokenOptions.FromTargetAudience(iapClientId), cancellationToken).ConfigureAwait(false)); }
private async Task Setup() { await GoogleCredential .FromFileAsync( CommonSecurityConstants.PathToFirestoreJson, default); database = await FirestoreDb .CreateAsync(CommonSecurityConstants.ProjectId); }
private static async Task <AndroidPublisherService> CreateServiceAsync(string appName, string serviceAccountFile) { var credentials = await GoogleCredential.FromFileAsync(serviceAccountFile, CancellationToken.None).ConfigureAwait(false); credentials = credentials.CreateScoped(new SingletonList <string>(AndroidPublisherService.Scope.Androidpublisher)); return(new AndroidPublisherService( new BaseClientService.Initializer { HttpClientInitializer = credentials, ApplicationName = appName })); }
private async Task <GoogleCredential> GetGoogleSheetCredentials(string credentialFilePath) { var credentialAsJson = Environment.GetEnvironmentVariable(GoogleApplicationCredentialsAsJson); if (credentialAsJson != null) { return(GoogleCredential.FromJson(credentialAsJson)); } return(credentialFilePath switch { null or "" => await GoogleCredential.GetApplicationDefaultAsync(), _ => await GoogleCredential.FromFileAsync(credentialFilePath, CancellationToken.None) });
public static async Task <DriveService> AuthGoogle(IConfiguration configuration) { var Scopes = new List <string> { DriveService.Scope.Drive }; var googleCredential = await GoogleCredential.FromFileAsync( configuration[MicroserviceConfigurationVariables.Google.CREDENTIALS], CancellationToken.None); googleCredential = googleCredential.CreateScoped(Scopes); googleCredential = googleCredential.CreateWithUser(configuration[MicroserviceConfigurationVariables.Google.USERNAME]); var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = googleCredential, ApplicationName = configuration[MicroserviceConfigurationVariables.Google.APPLICATION] }); return(service); }
public async Task FromFileAsync_ServiceAccountCredential() { var tempFile = Path.GetTempFileName(); try { File.WriteAllText(tempFile, DummyServiceAccountCredentialFileContents); var credential = await GoogleCredential.FromFileAsync(tempFile, CancellationToken.None); Assert.IsType <ServiceAccountCredential>(credential.UnderlyingCredential); Assert.True(credential.IsCreateScopedRequired); var serviceCred = (ServiceAccountCredential)credential.UnderlyingCredential; Assert.Equal("CLIENT_EMAIL", serviceCred.Id); Assert.Equal("PROJECT_ID", serviceCred.ProjectId); } finally { try { File.Delete(tempFile); } catch { } } }
static async Task Main(string[] args) { string importBucket = @"doc-processing-import-poc"; string exportBucket = @"documentprocessing_export_poc"; using (httpClient) { //httpClient.BaseAddress = new Uri("https://us-documentai.googleapis.com/v1beta3"); httpClient.DefaultRequestHeaders.Add("X-Goog-User-Project", "docsprocessing-poc"); httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoCache = true, NoStore = true }; Console.WriteLine("Hello World!"); //await GoogleServiceAccountToken(); var timestamp = Regex.Replace($"{DateTime.Now.ToShortDateString()}_{DateTime.Now.Hour}_{DateTime.Now.Minute}", @"[^\d]", "_"); var dName = $@"C:\2021_Local\OCR\Google Alpha\Beta3\Single Page Image Quality\{timestamp}"; if (!Directory.Exists(dName)) { Directory.CreateDirectory(dName); } string TempBucketFolder = $@"KeithVoelsTest/{timestamp}"; // Explicitly use service account credentials by specifying // the private key file. var credential = await GoogleCredential.FromFileAsync(serviceAccountJsonFile, CancellationToken.None); var storage = await StorageClient.CreateAsync(credential); //--------------------------------------------------------------------------------------------------------------- // Upload files //await DeleteDocumentObjects(storage, importBucket, "Mock"); // Delete the files that already exist in temp folder //await DeleteDocumentObjects(storage, exportBucket, "Mock"); // Delete the files that already exist in temp folder //var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2", @"*.tif").Select(f => new FileInfo(f)).ToList(); //files.AddRange(Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Split", @"*.tif").Select(f => new FileInfo(f))); //foreach (var file in files) //{ // await UploadDocumentObject(storage, importBucket, file.FullName, $"Mock/{file.Name}"); //} //await CallDocumentApi_HttpClient(files.Select(f => ($"{TempBucketFolder}{f.Name}", $"{TempBucketFolder}{f.Name}"))); //await DownloadDocumentObjects(storage, exportBucket, TempBucketFolder, dName); //--------------------------------------------------------------------------------------------------------------- // Process file already in the storage bucket //await DeleteDocumentObjects(storage, exportBucket, "KeithVoelsTest"); // Delete the files that already exist //var documentObjects = await GetAllDocumentObjects(storage, importBucket, @"Google Pilot Test Docs Batch 2 Single Page/"); //var documentObjects = await GetAllDocumentObjects(storage, importBucket, @"Google Pilot Test Docs Batch 2/"); //var documentNames = documentObjects.Where(d => d.Name.EndsWith("tif")).Select(d => d.Name).ToList(); //var directoryNames = Directory.GetDirectories($@"C:\2021_Local\OCR\Google Alpha\Beta3\Mixed\", "*", SearchOption.AllDirectories).Select(d => new DirectoryInfo(d)).Select(d => d.Name).ToList(); //////// Split and Classify //foreach (var f in documentNames) //{ // var shortName = Path.GetFileNameWithoutExtension(f); // if (!directoryNames.Contains(shortName)) // { // var pdr = new BatchRequest($"gs://doc-processing-import-poc/{f}", $"gs://documentprocessing_export_poc/{$"{TempBucketFolder}/{shortName}"}"); // await CallDocumentApi_HttpClient_BatchProcess(pdr); // } //} //await DownloadDocumentObjects(storage, exportBucket, "KeithVoelsTest", dName); //File.WriteAllText($@"{dName}\Summary.txt", string.Join('\n', Log.Select(l => l.ToString()))); //--------------------------------------------------------------------------------------------------------------- // Extract //var fileNames = new List<(string FileName, string classification)>() //{ // ("3459817085_21_2635078027_V_2", "w2_2019") //}; var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\SinglePage", @"*.tif").Select(f => new FileInfo(f)).ToList(); //var files = documentNames.Select(x => new FileInfo($@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Form1040\{x}")); //var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2", @"*.tif").Select(f => new FileInfo(f)).ToList(); //files.AddRange(Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Split", @"*.tif").Select(f => new FileInfo(f))); foreach (var doc in files) { //var doc = new FileInfo($@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Visually Split\{fileName.FileName}.tif"); if (!doc.Exists) { throw new FileNotFoundException(); } var pdr = new Request(doc); try { //var type = DocumentType.Form1040; //if (fileName.classification.Contains("w2")) //{ // type = DocumentType.W2; //} var json = await CallDocumentApi_HttpClient_Process(pdr, DocumentType.ImageQuality); var result = new FileInfo($@"{dName}\{doc.Name}"); if (!result.Directory.Exists) { result.Directory.Create(); } await File.WriteAllTextAsync($@"{dName}\{ Path.GetFileNameWithoutExtension(result.Name) }.json", json); } catch (Exception ex) { Console.WriteLine($"Exceptions: {ex.Message}"); await File.WriteAllTextAsync($@"{dName}\{Path.GetFileNameWithoutExtension(doc.Name)}.json", ex.Message); } } //--------------------------------------------------------------------------------------------------------------- // Process file already in the storage bucket // One Off - Download Documents //await DownloadDocumentObjects(storage, exportBucket, @"KeithVoelsTest", dName); } }