Google BigQuery with .NET documentation/ samples

前端 未结 2 1423
野的像风
野的像风 2020-12-05 20:25

I require to query data using Google BigQuery API. But I am struggling to find .NET Samples, and there was no documentation included with the binary (Google.Apis.Bigquer

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 21:26

    We don't yet have any BigQuery C# samples, but the Google .NET library comes with various samples for other Google APIs and the code is similar. See this page for an example.

    The code will look something like this below (note: I haven't had a chance to actually test this yet, so edits welcome...). By the way, we are in the middle of making lots of documentation updates for BigQuery, and we are hoping to post some C# samples as soon as we can (but most likely next month).

    using DotNetOpenAuth.OAuth2;
    using Google.Apis.Authentication.OAuth2;
    using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
    
    using Google.Apis.Bigquery.v2;
    using Google.Apis.Util;
    
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                // Register an authenticator.
                var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
    
                // Put your client id and secret here (from https://developers.google.com/console)
                // Use the installed app flow here.
                provider.ClientIdentifier = "";
                provider.ClientSecret = "";
    
                // Initiate an OAuth 2.0 flow to get an access token
                var auth = new OAuth2Authenticator(provider, GetAuthorization);
    
                // Create the service.
                var service = new BigqueryService(auth);
    
                // Do something with the BigQuery service here
                // Such as... service.[some BigQuery method].Fetch();
            }
    
            private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
            {
                // Get the auth URL:
                IAuthorizationState state = new AuthorizationState(new[] { BigqueryService.Scopes.Bigquery.GetStringValue() });
                state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
                Uri authUri = arg.RequestUserAuthorization(state);
    
                // Request authorization from the user (by opening a browser window):
                Process.Start(authUri.ToString());
                Console.Write("  Authorization Code: ");
                string authCode = Console.ReadLine();
                Console.WriteLine();
    
                // Retrieve the access token by using the authorization code:
                return arg.ProcessUserAuthorization(authCode, state);
            }
        }
    }
    

提交回复
热议问题