Google BigQuery asking Gmail Confirmation, Best way to handle in Production Environment

71 views Asked by At

I have created one C# console application to read BigQuery data.

While i am first running this console application, it opens browser window for asking Google acceptance, further times it does not asking permission in same machine.

Question: It asking permission for each and every machine for the first time, if yes which is the best way to deploy BigQuery application, bz client needs to accept Gmail access by using Developer ID? This makes sense or Which is the best way to handle Google BigQuery application in production environment?

Here is the code.

using Google.Apis.Auth.OAuth2;
using System.IO;
using System.Threading;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using System.Data;
using Google.Apis.Services;
using System;

namespace GoogleBigQuery
{
    public class Class1
    {
        private static void Main()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open,
                                            FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                   GoogleClientSecrets.Load(stream).Secrets,
                   new[] { BigqueryService.Scope.Bigquery },
                   "user", CancellationToken.None).Result;
            }

            //  Create and initialize the Bigquery service. Use the Project Name value
            //  from the New Project window for the ApplicationName variable.

            BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "PROJECT NAME"
            });

            string query = "YOUR QUERY";

            JobsResource j = Service.Jobs;
            QueryRequest qr = new QueryRequest();
            qr.Query = query;

            DataTable DT = new DataTable();
            int i = 0;

            QueryResponse response = j.Query(qr, "PROJECT ID").Execute();

            if (response != null)
            {
                int colCount = response.Schema.Fields.Count;

                foreach (var Column in response.Schema.Fields)
                {
                    DT.Columns.Add(Column.Name);
                }

                foreach (TableRow row in response.Rows)
                {
                    DataRow dr = DT.NewRow();

                    for (i = 0; i < colCount; i++)
                    {
                        dr[i] = row.F[i].V;
                    }

                    DT.Rows.Add(dr);
                }
            }
            else
            {
                Console.WriteLine("Response is null");
            }
        }
    }
}

Thanks, Selvakumar S

1

There are 1 answers

0
Felipe Hoffa On BEST ANSWER

I'm not an expert in C#, but I can see that you are using "GoogleWebAuthorizationBroker" - that's indeed a helper class that will open a web page to get the end-user authentication and authorization.

Use instead an OAuth2ServiceAccount for machine2machine auth (https://developers.google.com/identity/protocols/OAuth2ServiceAccount).

For sample C# code, see "where can i find ServiceAccountCredential".