2016-12-12 103 views
0

我正在使用bigquery进行身份验证。BigQuery在没有浏览器提示的情况下使用身份验证

我已经建立了一个认证密钥,如 'https://console.developers.google.com/apis/credentials?project= ......' - 的OAuth 2.0客户端ID,

我使用的Visual Studio 2015年,并从安装的NuGet:googles.apis.bigQuery.v2。

另外,我在包管理器控制台中写道:安装,包装Google.Cloud.BigQuery.V2 - 预

我试图连接到BIGQUERY - 我成功这样做,但是当我创建的凭证,我提示输入Gmail帐户登录名+接受某些访问权限范围权限。

我不想提示。


此外 - 在我的代码中,使用凭证登录时 - 即使我的凭证不正确,我成功连接到该项目。

这里是我的代码:

   projectId = "myproject"; 

      string[] scopes = new string[] { BigqueryService.Scope.Bigquery,    // view and manage your BigQuery data 
           BigqueryService.Scope.BigqueryInsertdata ,  // Insert Data into Big query 
           BigqueryService.Scope.CloudPlatform,   // view and manage your data acroos cloud platform services 
           BigqueryService.Scope.DevstorageFullControl, // manage your data on Cloud platform services 
           BigqueryService.Scope.DevstorageReadOnly ,  // view your data on cloud platform servies 
           BigqueryService.Scope.DevstorageReadWrite }; 
      using (var stream = new FileStream("clientsecret.json", FileMode.Open, FileAccess.Read)) 
      { 
       ClientSecrets cs = GoogleClientSecrets.Load(stream).Secrets; 

       GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer 
       { 

        ClientSecrets = cs, 
        Scopes = scopes, 
        DataStore = new FileDataStore("Store") 
       }); 
       UserCredential credentail; 
       credentail = GoogleWebAuthorizationBroker.AuthorizeAsync(
        cs, 
        scopes 
        , "username" 
        , CancellationToken.None 
        , new FileDataStore("Store")).Result; 
        // ***** Here the Chrome explorer is opened, and I asked to login to gmail etc... 
        // Nevertheless, I 
        // ... rest of code. 
      } 

这里是JSON文件:clientsecret.json

{ 
"installed": { 
"client_id": "xxx", 
"project_id": "xxx", 
"auth_uri": "https://accounts.google.com/o/oauth2/auth", 
"token_uri": "https://accounts.google.com/o/oauth2/token", 
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
"client_secret": "xxx", 
"redirect_uris": [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost" ] 
} 
} 

我一直在使用流量,并添加凭据尝试如下:

  GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer 
      { 

       ClientSecrets = cs, 
       Scopes = scopes, 
       DataStore = new FileDataStore("Store") 
      }); 

       TokenResponse tokenResponse = null; 
       //     tokenResponse = new TokenResponse { RefreshToken = "120" , ExpiresInSeconds = 0}; 
       tokenResponse = new TokenResponse { RefreshToken = "120", ExpiresInSeconds = -1}; 
    ... 
    credentail = new UserCredential(flow, "Spotoption-BG-other", tokenResponse); 

以上是代替之前的凭据设置。 当我试图像检索数据:

string query = "select ..."; 
JobsResource j = Service.Jobs; 
QueryRequest qr = new QueryRequest(); 
QueryResponse response = j.Query(qr, projectId).Execute(); 

我在最后一行异常:附加信息:错误: “invalid_grant”,说明: “” 乌里: “”

当我这样做:

bool b = flow.ShouldForceTokenRetrieval(); 

我得到了:false。

当tokenResponse = NULL,我得到的消息(后QueryResponse响应=):

Additional information: Object reference not set to an instance of an object.

+0

阅读关于如何使用谷歌apis oauth2。 –

+0

我已阅读。 关于这个问题没有太多好的文档或样本。 我提供的代码无法正常工作,因为它打开Chrome浏览器,并ClientSecret被忽略(不管我说,甚至是错误的密钥的行为是相同的)。 – Eitan

+0

在第一次运行时应该保存刷新标记的情况下打开浏览器是正常的。稍后调用不会打开浏览器。请遵循关于实施客户端oauth2流程的文档。 –

回答

1

要在不提示的浏览器使用谷歌的认证,你必须使用一个令牌。

该标记在第一次接受范围时返回,之后您可以保留该标记并将其发送回第二次使用的google-api。

刷新令牌对于用户是永久的。

这里是用于检索令牌第一时间代码(浏览器被打开用于接受范围的术语):

 public TokenResponse GetToken() 
    { 
     UserCredential credentail; 

     try 
     { 
      string[] scopes = new string[] { BigqueryService.Scope.Bigquery, // view and manage your BigQuery data 
           BigqueryService.Scope.BigqueryInsertdata , // Insert Data into Big query 
           BigqueryService.Scope.CloudPlatform,  // view and manage your data acroos cloud platform services 
           BigqueryService.Scope.DevstorageFullControl,// manage your data on Cloud platform services 
           BigqueryService.Scope.DevstorageReadOnly , // view your data on cloud platform servies 
           BigqueryService.Scope.DevstorageReadWrite }; 


      credentail = GoogleWebAuthorizationBroker.AuthorizeAsync(
       _clientSecret, 
       scopes, 
       "reports" 
       , CancellationToken.None 
       , new FileDataStore("Store") 
       ).Result; 
      if (credentail != null) 
       return credentail.Token; 
      else 
      { 
       _log.WriteToAll("Invalid credentials", EventLogEntryType.Error); 
       return null; 
      } 
     } 
     catch (Exception ex) 
     { 
      _log.WriteToAll(ex); 
      return null; 
     } 
    } 

检索令牌之后,它可以通过流动如我在给发送我的例如:

   TokenResponse tokenResponse = null; 
      tokenResponse = new TokenResponse 
      { 
       RefreshToken = refreshToken 
      }; 
相关问题