作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个控制台应用程序以从 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
不解析使用代码中的任何 using
。也许它包含在过时的 Google.Apis.Authentication
中。我们可以使用任何简单明了的代码来下载 BigQuery 查询结果吗?我想这里的主要问题是制作 auth 对象。
最佳答案
为了安装这个 nuget 包:
PM> Install-Package Google.Apis.Bigquery.v2
这是我通常使用的代码。
/// <summary>
/// Authenticate to Google Using Oauth2
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
/// <param name="userName">A string used to identify a user.</param>
/// <returns></returns>
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;
}
}
关于c# - NativeApplicationClient 和 OAuth2Authenticator 未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712366/
在我的 Visual Stuidio Win 窗体项目中使用此代码时。 var provider = new NativeApplicationClient(GoogleAuthenticationS
我已经引用了这个帖子 Google Authentication Process ,这与我的问题并不完全相似,但我使用的代码是相似的。 using System; using DotNetOpenAu
我正在编写一个控制台应用程序以从 BigQuery 下载数据。再一次,.NET 库含糊不清。 In this question ,两名 Google 员工 发布了回复,但这些回复都无法在我的机器上运行
我是一名优秀的程序员,十分优秀!