c# - 如何将 "&login_hint=user@gmail.com"附加到 GoogleWebAuthorizationBroker

标签 c# google-oauth google-api-dotnet-client

我想将 login_hint 附加到对 Google 的身份验证请求中。 我正在使用以下代码:

FileDataStore fDS = new FileDataStore(Logger.Folder, true);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                                clientSecrets.Secrets,
                                scopes.ToArray(),
                                username,
                                CancellationToken.None,
                                fDS).
                                Result;
var initializer = new Google.Apis.Services.BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;

我在哪里传递这个参数以便在浏览器打开之前附加邮件地址?

最佳答案

感谢 Zhaph 的提示!

到目前为止我的解决方案:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Requests;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace MyOAuth2
{
    //own implementation to append login_hint parameter to uri

    public class MyOAuth2WebAuthorizationBroker : GoogleWebAuthorizationBroker
    {
        public new static async Task<UserCredential> AuthorizeAsync(ClientSecrets clientSecrets,
            IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken,
            IDataStore dataStore = null)
        {
            var initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = clientSecrets,
            };
            return await AuthorizeAsyncCore(initializer, scopes, user, taskCancellationToken, dataStore)
                .ConfigureAwait(false);
        }

        private static async Task<UserCredential> AuthorizeAsyncCore(
            GoogleAuthorizationCodeFlow.Initializer initializer, IEnumerable<string> scopes, string user,
            CancellationToken taskCancellationToken, IDataStore dataStore = null)
        {
            initializer.Scopes = scopes;
            initializer.DataStore = dataStore ?? new FileDataStore(Folder);
            var flow = new MyAuthorizationCodeFlow(initializer, user);

            // Create an authorization code installed app instance and authorize the user.
            return await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync
                (user, taskCancellationToken).ConfigureAwait(false);
        }
    }

    public class MyAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
    {
        private readonly string userId;

        /// <summary>Constructs a new Google authorization code flow.</summary>
        public MyAuthorizationCodeFlow(Initializer initializer, string userId)
            : base(initializer)
        {
            this.userId = userId;
        }

        public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
        {
            return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
            {
                ClientId = ClientSecrets.ClientId,
                Scope = string.Join(" ", Scopes),
                //append user to url
                LoginHint = userId,
                RedirectUri = redirectUri
            };
        }
    }
}

关于c# - 如何将 "&login_hint=user@gmail.com"附加到 GoogleWebAuthorizationBroker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512300/

相关文章:

c# - Windows 7 中的rapi.dll 问题 - C#

c# - 在可移植库中使用适用于 .NET 的 Google API 客户端库

c# - purchase.products.get 忽略 productId 值并在 ProductPurchase 中返回 null

c# - ASP.NET Google Reporting API 值与 Google Analytics Dashboard 不同

c# - IList<mutable_struct> 与 mutable_struct[]

C#线程数组

c# - 将鼠标悬停在子节点上时,TreeViewItem 父节点和子节点 IsSelected 分离

php - Google+ 身份验证 : refreshToken() returns invalid_grant error

ios - 如何验证来自 iOS 客户端的 Google Cloud Endpoint 调用

python - 如何从 YouTube Analytics api 的 json 文件加载客户端凭据?