通过Windows Service进行验证步骤



我已经在大查询中创建了一个项目,并且在API Access Pane中,我创建了服务帐户,以便我可以代表用户交互使Windows应用程序可以访问大型查询API。由于我不熟悉和访问Google API,因此我想知道基本步骤,以便可以通过Windows Service访问大查询。

使用基于Web的Oauth Flow:Google BigQuery带有.NET文档/样本

有关使用.NET中的服务帐户的另一个DEV的示例,请参见此处:Google OAuth2服务帐户访问令牌请求给出"无效的请求"响应

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

这是与分析API一起使用的非官方样本。它应该与使用BigQuery使用服务帐户直接类似:我如何使用服务帐户使用.NET C#?

访问Google Analytics API V3

最后,这是对大查询API的身份验证过程的工作代码,并从大查询表中检索记录: -

我创建了一个具有返回类型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!" + "nn" + "Statement:- " + ex.Message.ToString() + "nn" + "Description:- " + ex.ToString() + "nn");
                Console.WriteLine("nPress enter to exit");
                Console.ReadLine();
            }

相关内容

  • 没有找到相关文章

最新更新