我想将 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;
我应该在哪里传递此参数,以便在浏览器打开之前附加邮件地址?
感谢扎夫的提示!
到目前为止我的解决方案:
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
};
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)