首页 > 解决方案 > 终端中的 GoogleWebAuthorizationBroker.AuthorizeAsync

问题描述

我正在使用常见的方法:

GoogleWebAuthorizationBroker.AuthorizeAsync

授权我对 Admob API 进行的一些调用。

在我的机器上,一切正常,因为我在 macos 上,当我需要输入身份验证信息时,浏览器会打开。

但是,我怎样才能在终端中做到这一点?目标服务器将有一个 ubuntu 终端并且没有安装 X。我应该为此使用 oauth2l 吗?

这是我用来进行初始登录的基本代码:

  var secrets = GoogleClientSecrets.Load(stream).Secrets;
            
            UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    secrets,
                    new[] {AdMobService.Scope.AdmobReport},
                    "user",
                    CancellationToken.None,
                    new FileDataStore("Store"));
            
            string temp = await credential.GetAccessTokenForRequestAsync();
            
            var service = new AdMobService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "MyApp",
            }); 
            
            var accounts = await service.Accounts.List().ExecuteAsync();

这是我将使用(尚未测试)来刷新令牌的代码:

TokenResponse token = new TokenResponse
            {
                RefreshToken = "XXXXX",
            };
            
            var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = secrets,
                Scopes = new[] {AdMobService.Scope.AdmobReport},
                DataStore = new FileDataStore("Store")
            });
            
            var credential = new UserCredential(flow, "user", token);

标签: .netgoogle-apigoogle-oauthgoogle-api-dotnet-client

解决方案


推荐阅读