1

我在 Big Query 和 API 访问窗格中创建了一个项目,我创建了服务帐户,以便我可以代表用户交互通过 Windows 应用程序访问 Big Query API。由于我是使用和访问 Google API 的新手,因此我想知道基本步骤,以便我可以通过 Windows 服务访问 Big Query。

4

2 回答 2

2

这里有一些使用基于 Web 的 OAuth 流的 .NET 代码示例: Google BigQuery with .NET documentation/samples

有关在 .NET 中使用服务帐户的另一个开发人员的示例,请参见: Google OAuth2 服务帐户访问令牌请求给出“无效请求”响应

这些在 .NET 库之上手动实现服务帐户。.NET 库最近添加了对服务帐户的本机支持,但我还没有找到官方示例。

这是一个使用 Analytics API 的非官方示例。它应该直接类似于在 BigQuery 中使用服务帐户: 如何使用服务帐户通过 .NET C# 访问 Google Analytics API V3?

于 2012-10-20T23:40:37.550 回答
0

最后,这是 Big Query API 的身份验证过程和从 Big Query 表中检索记录的工作代码:-

我创建了一个具有返回类型方法的类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();
            }
于 2012-11-07T06:23:08.803 回答