C# · 12月 29, 2021

c# – 如何为GoogleWebAuthorizationBroker.AuthorizeAsync设置return_uri?

我试图在我的非MVC .NET Web应用程序中使用 Google Calendar API. (这似乎是一个重要的区别.)

我试图使用Google的this example和Daimto的this example的代码,以及一些related posts here的一些有用的提示.

我写了以下方法:

public void GetUserCredential( String userName ){ String clientId = ConfigurationManager.AppSettings[ “Google.ClientId” ]; //From Google Developer console https://console.developers.google.com String clientSecret = ConfigurationManager.AppSettings[ “Google.ClientSecret” ]; //From Google Developer console https://console.developers.google.com String[] scopes = new string[] { Google.Apis.Calendar.v3.CalendarService.Scope.Calendar }; // here is where we Request the user to give us access,or use the Refresh Token that was prevIoUsly stored in %AppData% UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = clientId,ClientSecret = clientSecret },scopes,userName,CancellationToken.None,new FileDataStore( “c:\\temp” ) ).Result; // TODO: Replace FileDataStore with DatabaseDataStore}

问题是,当Google的OAuth2页面被调用时,redirect_uri将继续设置为http:// localhost:< some-random-port> / authorize.我不知道如何将其设置为其他内容,如AuthorizeAsync生成的以下示例URL所示:

https://accounts.google.com/o/oauth2/auth?access_type=offline &response_type=code &client_id=********.apps.googleusercontent.com &redirect_uri=http:%2F%2Flocalhost:40839%2Fauthorize%2F &scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar

Google会通过一个redirect_uri_mismatch错误页面回复一条消息:

“请求中的重定向URI:http://localhost:XXXXX/authorize/与注册的重定向URI不匹配”

我只能在我的Google开发人员控制台凭证页面中注册这么多重定向URI.我不想注册65535个端口,我想在我的网站上使用除/授权之外的页面.具体来说,在开发过程中,我想使用http:// localhost:888 / Pages / GoogleApiRedirect,但是不知道我在哪里设置这个,除了我在开发者控制台中所做的一切.

如何显式设置redirect_uri的值?我也以“这种做法完全错误”的形式开放回应.

编辑:

在过去一天玩过之后,我发现通过使用本机应用程序的客户端ID /客户端密钥而不是Web应用程序,我至少可以访问Google的Web授权页面,而不会抱怨redirect_uri_mismatch.这仍然是不可接受的,因为它仍然返回到http:// localhost:< some-random-port> / authorize,这在我的Web应用程序的控制之外.

解决方法 你可以使用这段代码:(原始想法从 http://coderissues.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker) dsAuthorizationBroker.RedirectUri = “my localhost redirect uri”;UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync(…

dsAuthorizationBroker.cs

using System;using System.Collections.Generic;using System.Threading;using System.Threading.Tasks;using Google.Apis.Auth.OAuth2;using Google.Apis.Auth.OAuth2.Flows;using Google.Apis.Auth.OAuth2.Requests;using Google.Apis.Util.Store;namespace OAuth2{ public class dsAuthorizationBroker : GoogleWebAuthorizationBroker { public static string RedirectUri; 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,user,taskCancellationToken,dataStore).ConfigureAwait(false); } private static async Task<UserCredential> AuthorizeAsyncCore( GoogleAuthorizationCodeFlow.Initializer initializer,IDataStore dataStore) { initializer.Scopes = scopes; initializer.DataStore = dataStore ?? new FileDataStore(Folder); var flow = new dsAuthorizationCodeFlow(initializer); return await new AuthorizationCodeInstalledApp(flow,new LocalServerCodeReceiver()) .AuthorizeAsync(user,taskCancellationToken).ConfigureAwait(false); } } public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow { public dsAuthorizationCodeFlow(Initializer initializer) : base(initializer) { } public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri) { return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri); } } }