C# 如何附加“&;登录提示=user@gmail.com&引用;谷歌网站授权经纪人
我想将登录提示附加到Google的身份验证请求中。 我正在使用以下代码:C# 如何附加“&;登录提示=user@gmail.com&引用;谷歌网站授权经纪人,c#,google-oauth,google-api-dotnet-client,C#,Google Oauth,Google Api Dotnet Client,我想将登录提示附加到Google的身份验证请求中。 我正在使用以下代码: FileDataStore fDS = new FileDataStore(Logger.Folder, true); GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream); credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
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;
在何处传递此参数,以便在浏览器打开之前追加邮件地址?查找.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)]
在类中(由代理中的authorizationAsyncCore
调用),方法CreateAuthorizationCodeRequest
确实调用GoogleAuthorizationCodeRequestUrl
,但它只设置ClientId
、范围
和重定向URL
如果您想在该类上设置附加属性,您可能需要编写自己的代理服务实例。感谢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.api.Auth.OAuth2;
使用Google.api.Auth.OAuth2.Flows;
使用Google.api.Auth.OAuth2.Requests;
使用Google.api.Util.Store;
使用制度;
使用System.Collections.Generic;
使用系统线程;
使用System.Threading.Tasks;
命名空间MyOAuth2
{
//将登录提示参数附加到uri的自己的实现
公共类MyAuth2WebAuthorizationBroker:GoogleWebAuthorizationBroker
{
public新静态异步任务AuthorizeAsync(ClientSecrets ClientSecrets,
IEnumerable作用域、字符串用户、CancellationToken任务CancellationToken、,
IDataStore数据存储=空)
{
var initializer=新的GoogleAuthorizationCodeFlow.initializer
{
ClientSecrets=ClientSecrets,
};
return Wait AuthorizationAsyncCore(初始值设定项、作用域、用户、taskCancellationToken、数据存储)
.配置等待(错误);
}
专用静态异步任务授权AsyncCore(
GoogleAuthorizationCodeFlow.初始值设定项初始值设定项IEnumerable作用域字符串用户,
CancellationToken任务CancellationToken,IDataStore数据存储=null)
{
initializer.Scopes=范围;
initializer.DataStore=数据存储??新文件数据存储(文件夹);
var flow=新的MyAuthorizationCodeFlow(初始值设定项,用户);
//创建已安装应用程序实例的授权码并授权用户。
return wait new AuthorizationCodeInstalledApp(流,new LocalServerCodeReceiver()).authorizationAsync
(用户,taskCancellationToken).ConfigureWait(false);
}
}
公共类MyAuthorizationCodeFlow:GoogleAuthorizationCodeFlow
{
私有只读字符串用户标识;
///构造一个新的Google授权代码流。
公共MyAuthorizationCodeFlow(初始值设定项、字符串用户ID)
:base(初始值设定项)
{
this.userId=userId;
}
公共覆盖AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(字符串重定向URI)
{
返回新的GoogleAuthorizationCodeRequestUrl(新Uri(AuthorizationServerUrl))
{
ClientId=ClientSecrets.ClientId,
Scope=string.Join(“,Scopes),
//将用户附加到url
LoginHint=userId,
重定向URI=重定向URI
};
}
}
}
好的,谢谢您的提示,但是在中有一个属性“login\u hint”。但是我如何在GoogleWebAuthorizationBroker中使用它呢?您可能需要根据GoogleAuthorizationCodeRequest而不是使用Broker helper类来制定自己的请求。谢谢您的提示!非常感谢您的实施!节省了我很多时间。可怜的是,谷歌一开始没有正确处理这件事。