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.Bigquery.dll). Can anybody provide me with sample usage for .NET?
-
Please see the answers below-- did they help? – Ryan Boyd Sep 18 '12 at 07:14
-
Let us know if you need more help. If the answers below work, please vote up/accept. Thx! – Ryan Boyd Oct 20 '12 at 23:19
-
For a recent one, see http://bitvectors.blogspot.de/2014/05/front-end-google-bigquery-with-aspnet_27.html – Felipe Hoffa Jun 18 '14 at 04:12
2 Answers
Here's a working sample, based in part off of Michael's response:
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;
using Google.Apis.Util;
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace BigQueryConsole
{
public class BigQueryConsole
{
// Put your client ID and secret here (from https://developers.google.com/console)
// Use the installed app flow here.
// Client ID looks like "9999999.apps.googleusercontent.com"
static string clientId = "YOURCLIENTID";
static string clientSecret = "YOURSECRET";
// Project ID is in the URL of your project on the APIs Console
// Project ID looks like "999999";
static string projectId = "YOURPROJECTID";
// Query in SQL-like form
static string query = "SELECT state, count(*) from [publicdata:samples.natality] GROUP BY state ORDER BY state ASC";
public static void Main(string[] args)
{
// Register an authenticator.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = clientId;
provider.ClientSecret = clientSecret;
// Initiate an OAuth 2.0 flow to get an access token
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service.
var service = new BigqueryService(auth);
JobsResource j = service.Jobs;
QueryRequest qr = new QueryRequest();
qr.Query = query;
QueryResponse response = j.Query(qr, projectId).Fetch();
foreach (TableRow row in response.Rows)
{
List<string> list = new List<string>();
foreach (TableRow.FData field in row.F)
{
list.Add(field.V);
}
Console.WriteLine(String.Join("\t", list));
}
Console.WriteLine("\nPress enter to exit");
Console.ReadLine();
}
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);
}
}
}
This uses synchronous queries. For asyncronous queries, the code would be slightly different.
You'll need to reference both the BigQuery service dll (under 'Services' directory in the binary download), plus the other dlls in the 'Lib' directory. Binary release is here: http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release
The .NET code is going to be very similar to the Java library code (they're generated off of the same API description): https://developers.google.com/bigquery/docs/developers_guide#batchqueries
We'll get more samples out there soon, but hopefully this helps in the meantime.

- 2,978
- 1
- 21
- 19
-
for a recent one, see http://bitvectors.blogspot.de/2014/05/front-end-google-bigquery-with-aspnet_27.html – Felipe Hoffa Jun 18 '14 at 04:14
-
The library you have linked requires Windows 8.1 and all its NuGet packages need to be resolved. I saw some of your videos on YouTube about Big Query. The `using` statements used in your code snippet above does not work with the rest of the code. If the Java library is more stable please let me know so we switch to that. – disasterkid Sep 21 '15 at 14:07
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 = "<client id>";
provider.ClientSecret = "<client secret>";
// Initiate an OAuth 2.0 flow to get an access token
var auth = new OAuth2Authenticator<NativeApplicationClient>(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);
}
}
}

- 7,931
- 6
- 33
- 47
-
4Have these samples or documentation been made yet? I still can't seem to track them down, I've found the API docs which is nice, but doesn't really give a good starting point! – Ben Jul 24 '13 at 08:24
-
what is Google.Apis.Util, GoogleAuthenticationServer, NativeApplicationClient please post some explanation for dependencies. Also, any chance you have samples? – Cherven Oct 08 '14 at 19:30
-
When I download the latest Google Api library via NuGet https://www.nuget.org/packages/Google.Apis/ it does not recognize `Google.Apis.Authentication`. Is this a working version of the code you have pasted above? If the Java library is less of a hassle please let us know so we switch to that. – disasterkid Sep 21 '15 at 14:22
-
1Fixed with http://stackoverflow.com/questions/32712366/nativeapplicationclient-and-oauth2authenticator-not-resolved – akdora Dec 10 '16 at 15:06