How to append “&login_hint=user@gmail.com” to GoogleWebAuthorizationBroker

只谈情不闲聊 提交于 2020-01-02 05:38:06

问题


I want to append the login_hint to an authentication request to Google. I'm using the following code:

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;

Where do I pass this parameter so the mail address is appended before the browser opens?


回答1:


thanks for the hints by Zhaph!

My solution so far:

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



回答2:


Looking at the source code for the .NET library, that parameter isn't supported.

The following query string parameters are currently defined on the Authorisation Request 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)]

Within the GoogleAuthorizationCodeFlow class (which is called by AuthorizeAsyncCore within the broker), the method CreateAuthorizationCodeRequest does indeed call GoogleAuthorizationCodeRequestUrl but it only sets the ClientId, Scope and RedirectUrl.

It looks like you'll need to write your own instance of the Broker service if you want to set the additional properties on that class.



来源:https://stackoverflow.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!