【发布时间】:2015-12-19 03:57:54
【问题描述】:
我正在编写一个控制台应用程序来从 BigQuery 下载数据。再一次,.NET 库是模糊和混乱的。 In this question,两名 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 = "<client id>";
provider.ClientSecret = "<client secret>";
// Initiate an OAuth 2.0 flow to get an access token
var auth = new OAuth2Authenticator<NativeApplicationClient>(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);
}
}
}
- 首先,
Google.Apis.Authentication现在已经过时并且 NuGet 鼓励您改用Google.Api.Auth。 -
NativeApplicationClient无法使用代码中的任何usings 进行解析。也许它被包含在过时的Google.Apis.Authentication中。 - 其中一名员工发布了一个链接 (https://github.com/google/google-api-dotnet-client#Latest_Stable_Release),指向代码的 Github 存储库。但是这个 repo 中的大多数项目都需要我没有的 Windows 8.1。
有没有什么简单明了的代码可以用来下载 BigQuery 查询结果?我想这里的主要问题是制作 auth 对象。
【问题讨论】:
-
你读过developers.google.com/api-client-library/dotnet/guide/aaa_oauth 吗?我相信你是对的,
NativeApplicationClient在旧的 auth nuget 包中。我相信你现在想要GoogleWebAuthorizationBroker.AuthorizeAsync。
标签: c# oauth-2.0 google-api google-bigquery google-api-dotnet-client