NativeApplicationClient和OAuth2Authenticator未解析 [英] NativeApplicationClient and OAuth2Authenticator not resolved

查看:166
本文介绍了NativeApplicationClient和OAuth2Authenticator未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个控制台应用程序来从BigQuery下载数据。 .NET库再一次是模糊和混乱的。 在此问题中两名Google员工已发布一个响应,并且这两个响应都没有在我的机器上运行,因为他们还没有清楚地说明他们正在使用的引用。我再次粘贴代码并进行详细说明:

使用DotNetOpenAuth.OAuth2;

  
使用Google.Apis.Authentication.OAuth2;
使用Google.Apis.Authentication.OAuth2.DotNetOpenAuth;

使用Google.Apis.Bigquery.v2;
使用Google.Apis.Util;

{
public class Program
{
public static void Main(string [] args)
{
//注册一个认证者。
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);

//将您的客户ID和秘密放在这里(来自https://developers.google.com/console)
//在这里使用已安装的应用程序流程。
provider.ClientIdentifier =< client id>;
provider.ClientSecret =< client secret>;

//启动OAuth 2.0流程以获取访问令牌
var auth = new OAuth2Authenticator< NativeApplicationClient>(provider,GetAuthorization);

//创建服务。
var service = new BigqueryService(auth);

//在这里使用BigQuery服务做一些事情
//比如... service。[一些BigQuery方法] .Fetch();
}

私有静态IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
//获取授权URL:
IAuthorizationState state = new AuthorizationState(new [] {BigqueryService.Scopes.Bigquery.GetStringValue()});
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);

//请求来自用户的授权(通过打开浏览器窗口):
Process.Start(authUri.ToString());
Console.Write(Authorization Code:);
string authCode = Console.ReadLine();
Console.WriteLine();

//使用授权码检索访问令牌:
return arg.ProcessUserAuthorization(authCode,state);
}
}
}




  1. 首先, Google.Apis.Authentication 现在已经过时,NuGet
    鼓励您使用 Google.Api.Auth
  2. NativeApplicationClient 不会使用任何使用 s在代码中。可能它包含在过时的 Google.Apis.Authentication 中。

  3. 其中一位员工发布了一个链接( https://github.com/google/google-api-dotnet-client#Latest_Stable_Release )到代码的Github回购。但是这个回购库中的大多数项目都需要Windows 8.1,这是我没有的。

是否有任何直接明了的代码可供我们使用下载 BigQuery 查询结果?我猜这里的主要问题是制作 auth 对象。为了安装这个nuget包:


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


这是我通常使用的代码。

  ///< summary> 
///使用Oauth2
进行身份验证///文档https://developers.google.com/accounts/docs/OAuth2
///< / summary>
///< param name =clientId>从Google Developer Console https://console.developers.google.com< / param>
///< param name =clientSecret>从Google Developer Console https://console.developers.google.com< / param>
///< param name =userName>用于识别用户的字符串。< / param>
///<返回>< /返回>
public static BigqueryService AuthenticateOauth(string clientId,string clientSecret,string userName)
{

string [] scopes = new string [] {BigqueryService.Scope.Bigquery,// view并管理您的BigQuery数据
BigqueryService.Scope.BigqueryInsertdata,//将数据插入Big查询
BigqueryService.Scope.CloudPlatform,//查看和管理您的数据acroos云平台服务
BigqueryService.Scope .DevstorageFullControl,//在Cloud平台服务上管理您的数据
BigqueryService.Scope.DevstorageReadOnly,//在云平台服务上查看您的数据
BigqueryService.Scope.DevstorageReadWrite}; //在云平台服务上管理您的数据

尝试
{
//这里是我们要求用户给我们访问的地方,或者使用先前存储的刷新令牌in%AppData%
UserCredential凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(新ClientSecrets {ClientId = clientId,ClientSecret = clientSecret}
,范围
,用户名
,CancellationToken.None
,新的FileDataStore(Daimto.BigQuery.Auth.Store))。

BigqueryService service = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName =BigQuery API示例,
} );
退货服务;
}
catch(Exception ex)
{

Console.WriteLine(ex.InnerException);
返回null;

}

}


I am writing a Console Application to download data from BigQuery. Once again, the .NET library is vague and confusing. In this question, two Google employees have posted a response and neither of the responses is working on my machine because they haven't quite made it clear which references they are using. I paste the code once again and elaborate:

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);
        }
    }
}

  1. First, the Google.Apis.Authentication is now obsolete and NuGet encourages you to use Google.Api.Auth instead.
  2. The NativeApplicationClient does not resolve using any of the usings in the code. Maybe it was included in the obsolete Google.Apis.Authentication.
  3. One of the employees has posted a link (https://github.com/google/google-api-dotnet-client#Latest_Stable_Release) to the Github repo of the code. But most projects in this repo demand Windows 8.1 which I don't have.

Is there any straightforward and clear code we could use for downloading BigQuery query results? I guess the main problem here is making the auth object.

解决方案

In order to install this nuget package:

PM> Install-Package Google.Apis.Bigquery.v2

This is the code I normally use.

/// <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;

    }

}

这篇关于NativeApplicationClient和OAuth2Authenticator未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆