使用.NET Google.Apis.Oauth2.v2指定login_hint

时间:2015-11-29 11:44:00

标签: google-oauth google-api-dotnet-client

由于Google.Apis.Oauth2.v2中的GoogleWebAuthorizationBroker.AuthorizeAsync()请求uri到包含Google Oauth2服务的链接和参数的静态json文件,如果碰巧我怎么能指定login_hint参数提前知道这些信息吗?

var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
    new Uri("ms-appx:///Assets/client_secret.json"),
    myScopes,
    "user",
    CancellationToken.None);

摘自client_secret.json

"auth_uri":"https://accounts.google.com/o/oauth2/auth?login_hint=user@domain.com" 
  • 如何根据每个用户指定login_hint参数?

1 个答案:

答案 0 :(得分:1)

我最终继承了Google网络授权代理的子类:

public class MyOAuth2WebAuthorizationBroker : GoogleWebAuthorizationBroker
{
    public static async Task<UserCredential> AuthorizeAsync(ClientSecrets clientSecrets,
        IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken)
    {
        var initializer = new MyOAuth2AuthorizationCodeFlow.Initializer
        {
            ClientSecrets = clientSecrets,
            Scopes = scopes,
            DataStore = new StorageDataStore(),
        };

        var installedApp = new AuthorizationCodeWindowsInstalledApp(new MyOAuth2AuthorizationCodeFlow(initializer, user));
        return await installedApp.AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
    }

    public class MyOAuth2AuthorizationCodeFlow : GoogleAuthorizationCodeFlow
    {
        private string loginHint { get; set; }
        public MyOAuth2AuthorizationCodeFlow(Initializer initializer, string loginHint) : base(initializer)
        {
            this.loginHint = loginHint;
        }

        public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
        {
            return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
            {
                ClientId = ClientSecrets.ClientId,
                Scope = string.Join(" ", Scopes),
                RedirectUri = redirectUri,
                LoginHint = this.loginHint
            };
        }
    }

}