如何将“&login_hint = user@gmail.com”附加到GoogleWebAuthorizationBroker?(涉及IT技术)

4

我想在向 Google 发送认证请求时添加 login_hint。 我使用以下代码:

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;

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

2个回答

3
感谢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
            };
        }
    }
}

非常感谢您的实现!省了我大量时间。遗憾的是,Google一开始没有正确处理这个问题。 - Derek Wade

0

查看 .NET 库的源代码时,发现该参数不受支持。

授权请求 URL 上当前定义了以下查询字符串参数:

[Google.Apis.Util.RequestParameterAttribute("response_type", Google.Apis.Util.RequestParameterType.Query)]

[Google.Apis.Util.RequestParameterAttribute("client_id", Google.Apis.Util.RequestParameterType.Query)]

[Google.Apis.Util.RequestParameterAttribute("redirect_uri", Google.Apis.Util.RequestParameterType.Query)]

[Google.Apis.Util.RequestParameterAttribute("scope", Google.Apis.Util.RequestParameterType.Query)]

[Google.Apis.Util.RequestParameterAttribute("state", Google.Apis.Util.RequestParameterType.Query)]

GoogleAuthorizationCodeFlow类中(由代理程序内的AuthorizeAsyncCore调用),方法CreateAuthorizationCodeRequest确实调用了GoogleAuthorizationCodeRequestUrl,但它仅设置了ClientIdScopeRedirectUrl
看起来如果您想在该类上设置其他属性,则需要编写自己的代理服务实例。

好的,谢谢你的提示。但是在 源代码 中有一个属性名为“login_hint”。那么我如何在使用GoogleWebAuthorizationBroker时使用它呢? - Ronny
1
你可能需要根据GoogleAuthorizationCodeRequest自己制定请求,而不是使用Broker助手类。 - Zhaph - Ben Duguid
1
谢谢您的提示! - Ronny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接