2012-10-19 27 views
1

我在Big Query中創建了一個項目,並在API Access窗格中創建了服務帳戶,以便我可以通過代表用戶交互的Windows應用程序訪問Big Query API。由於我是使用和訪問Google API的新手,我想知道基本步驟,以便通過Windows服務訪問Big Query。通過Windows服務驗證BigQuery的步驟

回答

2

這裏有使用基於網絡的一些的OAuth .NET代碼示例流量: Google BigQuery with .NET documentation/ samples

爲了從在.NET中使用服務帳戶的另一dev的一個例子,請參閱本: Google OAuth2 Service Account Access Token Request gives 'Invalid Request' Response

這些手動實現在.NET庫頂部的服務帳戶。 .NET庫最近添加了對服務帳戶的本機支持,儘管我還沒有找到官方的例子。

這是一個非官方樣本,與Analytics API一起工作。它應該是直接類似於使用服務帳戶與BigQuery: How do I use a Service Account to Access the Google Analytics API V3 with .NET C#?

+0

非常感謝,我能夠生成有效的訪問令牌,但現在的問題是,如何通過這個訪問令牌到Big Query服務對象,以便我可以與Big Query API進行通信。我嘗試從我身邊以及我搜索大部分網站。 –

0

最後在這裏是身份驗證過程中大查詢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!" + "\n\n" + "Statement:- " + ex.Message.ToString() + "\n\n" + "Description:- " + ex.ToString() + "\n\n"); 
       Console.WriteLine("\nPress enter to exit"); 
       Console.ReadLine(); 
      } 
相關問題