NativeApplicationClient和OAuth2Authenticator未解析

我正在编写一个控制台应用程序来从BigQuery下载数据。 再一次,.NET库是模糊和混乱的。 在这个问题中 , 两名Google员工发布了一个回复,但我的机器上没有任何响应正在进行,因为他们还没有明确说明他们正在使用哪些参考。 我再次粘贴代码并详细说明:

using DotNetOpenAuth.OAuth2; using Google.Apis.Authentication.OAuth2; using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; using Google.Apis.Bigquery.v2; using Google.Apis.Util; { public class Program { public static void Main(string[] args) { // Register an authenticator. var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description); // Put your client id and secret here (from https://developers.google.com/console) // Use the installed app flow here. provider.ClientIdentifier = ""; provider.ClientSecret = ""; // Initiate an OAuth 2.0 flow to get an access token var auth = new OAuth2Authenticator(provider, GetAuthorization); // Create the service. var service = new BigqueryService(auth); // Do something with the BigQuery service here // Such as... service.[some BigQuery method].Fetch(); } private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) { // Get the auth URL: IAuthorizationState state = new AuthorizationState(new[] { BigqueryService.Scopes.Bigquery.GetStringValue() }); state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); Uri authUri = arg.RequestUserAuthorization(state); // Request authorization from the user (by opening a browser window): Process.Start(authUri.ToString()); Console.Write(" Authorization Code: "); string authCode = Console.ReadLine(); Console.WriteLine(); // Retrieve the access token by using the authorization code: return arg.ProcessUserAuthorization(authCode, state); } } } 
  1. 首先, Google.Apis.Authentication现已过时,NuGet鼓励您使用Google.Api.Auth
  2. NativeApplicationClient无法使用代码中的任何using解析。 也许它被包含在过时的Google.Apis.Authentication
  3. 其中一名员工已在代码的Github回购中发布了一个链接( https://github.com/google/google-api-dotnet-client#Latest_Stable_Release )。 但是这个回购中的大多数项目都需要Windows 8.1,而我没有。

我们可以使用任何简单明了的代码来下载BigQuery查询结果吗? 我想这里的主要问题是制作auth对象。

为了安装这个nuget包:

PM>安装包Google.Apis.Bigquery.v2

这是我通常使用的代码。

 ///  /// Authenticate to Google Using Oauth2 /// Documentation https://developers.google.com/accounts/docs/OAuth2 ///  /// From Google Developer console https://console.developers.google.com /// From Google Developer console https://console.developers.google.com /// A string used to identify a user. ///  public static BigqueryService AuthenticateOauth(string clientId, string clientSecret, string userName) { 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 }; // manage your data on cloud platform servies try { // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } , scopes , userName , CancellationToken.None , new FileDataStore("Daimto.BigQuery.Auth.Store")).Result; BigqueryService service = new BigqueryService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "BigQuery API Sample", }); return service; } catch (Exception ex) { Console.WriteLine(ex.InnerException); return null; } }