Big Queryと[APIアクセス]ペインでプロジェクトを作成しました。これにより、ユーザーの操作に代わってWindowsアプリケーションからBigQueryAPIにアクセスできるようになります。Google APIの使用とアクセスは初めてなので、Windowsサービスを介してBigQueryにアクセスするための基本的な手順を知りたいと思います。
2 に答える
WebベースのOAuthフローを使用した.NETコードサンプルがいくつかあります: .NETドキュメント/サンプルを使用したGoogle BigQuery
.NETでサービスアカウントを使用している別の開発者の例については、次を参照してください: GoogleOAuth2サービスアカウントアクセストークンリクエストは「無効なリクエスト」レスポンスを提供します
これらは、.NETライブラリの上にサービスアカウントを手動で実装します。.NETライブラリは最近、サービスアカウントのネイティブサポートを追加しましたが、公式の例はまだ見つかりません。
これは、AnalyticsAPIで動作する非公式のサンプルです。BigQueryでサービスアカウントを使用するのと直接類似している必要があります。サービスアカウントを 使用して.NETC#でGoogle Analytics API V3にアクセスするにはどうすればよいですか?
最後に、Big Query APIへの認証プロセスと、BigQueryテーブルからレコードを取得するための作業コードを次に示します。
returntypeのメソッドを持つクラスを作成しましたOAuth2Authenticator<AssertionFlowClient>
。
internal class clsGetOAuth2Authentication
{
public OAuth2Authenticator<AssertionFlowClient> objGetOAuth2(string strPrivateFilePath, string strPrivateFilePassword, string strServiceAccEmailId,string strScope)
{
AuthorizationServerDescription objAuthServerDesc;
X509Certificate2 objKey;
AssertionFlowClient objClient;
OAuth2Authenticator<AssertionFlowClient> objAuth = null;
string ScopeUrl = "https://www.googleapis.com/auth/" + strScope;
string strSrvAccEmailId = strServiceAccEmailId;
string strKeyFile = strPrivateFilePath; //KeyFile: This is the physical path to the key file you downloaded when you created your Service Account.
string strKeyPassword = (strPrivateFilePassword != "") ? strPrivateFilePassword : "notasecret"; //key_pass: This is probably the password for all key files, but if you're given a different one, use that.
objAuthServerDesc = GoogleAuthenticationServer.Description; //objAuthServerDesc: Description of the server that will grant Authentiation.
objKey = new X509Certificate2(strKeyFile, strKeyPassword, X509KeyStorageFlags.Exportable); //objkey: Load up and decrypt the key.
objClient = new AssertionFlowClient(objAuthServerDesc, objKey) { ServiceAccountId = strSrvAccEmailId, Scope = ScopeUrl }; //objClient: Using the AssertionFlowClient, because we're logging in with our certificate.
objAuth = new OAuth2Authenticator<AssertionFlowClient>(objClient, AssertionFlowClient.GetState); //objAuth: Requesting Authentication.
return objAuth;
}
}
ここで、別のクラスが上記のメソッドを呼び出します。-
clsGetOAuth2Authentication objOAuth2 = new clsGetOAuth2Authentication();
try
{
var objAuth = objOAuth2.objGetOAuth2(strKeyFile, strKeyPassword, strSrvAccEmailId, strScope); //Authentication data returned
objBQServ = new BigqueryService(objAuth); //Instantiate BigQueryService with credentials(objAuth) as its parameter
#region Retrieving Records:-
JobsResource j = objBQServ.Jobs;
QueryRequest qr = new QueryRequest();
qr.Query = strQuery;
DateTime dtmBegin = DateTime.UtcNow;
QueryResponse response = j.Query(qr, strProjId).Fetch();
DateTime dtmEnd = DateTime.UtcNow;
string strColHead = "";
foreach (var colHeaders in response.Schema.Fields)
{
strColHead += colHeaders.Name.ToString() + "\t";
}
Console.WriteLine(strColHead);
int intCount = 0;
foreach (TableRow row in response.Rows)
{
intCount += 1;
List<string> list = new List<string>();
foreach (var field in row.F)
{
list.Add(field.V);
}
Console.WriteLine(String.Join("\t", list));
}
TimeSpan tsElapsed = dtmEnd - dtmBegin;
Console.WriteLine("\n" + "Total no. of records:- " + intCount + ". Time taken:- " + tsElapsed);
#endregion
}
catch (Exception ex)
{
Console.WriteLine("Error Occured!" + "\n\n" + "Statement:- " + ex.Message.ToString() + "\n\n" + "Description:- " + ex.ToString() + "\n\n");
Console.WriteLine("\nPress enter to exit");
Console.ReadLine();
}