GoogleWebAuthorizationBroker.AuthorizeAsync如何设置return_uri?我正在尝试在非MVC.NET网络应用程序中使用GoogleCalendarAPI。(这似乎是一个重要的区别。)我正在尝试使用来自Google和Daimto的示例中的代码以及来自此处一些相关帖子的一些有用提示。我写了以下方法:publicvoidGetUserCredential(StringuserName){StringclientId=ConfigurationManager.AppSettings["Google.ClientId"];//来自谷歌开发者控制台https://console.developers.google.comStringclientSecret=ConfigurationManager.AppSettings["Google.ClientSecret"];//来自谷歌开发者控制台https://console.developers.google.comString[]scopes=newstring[]{Google.Apis.Calendar.v3.CalendarService.Scope.Calendar};//这里是我们请求用户授予我们访问权限的地方,或者使用之前存储在%AppData%UserCredential中的刷新令牌credential=GoogleWebAuthorizationBroker.AuthorizeAsync(newClientSecrets{ClientId=clientId,ClientSecret=clientSecret},scopes,userName,CancellationToken.None,newFileDataStore("c:\temp")).Result;//TODO:ReplaceFileDataStorewithDatabaseDataStore}问题是,当调用Google的OAuth2页面时,redirect_uri会直接设置为http://localhost:/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%2FcalendarGoogle回复redirect_uri_mismatch错误页面,其中包含以下消息:“请求中的重定向URI:http://localhost:XXXXX/authorize/与已注册的重定向URI不匹配”我只能在GoogleDeveloper的控制台凭据页面中注册这么多重定向URI。我不打算注册端口65535,我想在我的网站上使用除/authorize之外的页面。具体来说,我想在开发过程中使用http://localhost:888/Pages/GoogleApiRedirect但我不知道在哪里设置它,除了我在开发者控制台中所做的。如何显式设置redirect_uri的值?我也愿意以“这种方法是完全错误的”的形式回复。编辑:在过去一天玩这个之后,我发现通过使用本机应用程序的客户端ID/客户端密码而不是网络应用程序,我至少可以访问谷歌的网络授权页面而不会抱怨redirect_uri_mismatch。这仍然是不可接受的,因为它仍然返回到http://localhost:/authorize,这超出了我的Web应用程序的控制范围。您可以使用此代码:(来自http://coderisissues.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker的原始想法)dsAuthorizationBroker.RedirectUri="mylocalhostredirecturi";UserCredentialcredential=awaitdsAuthorizationBroker.AuthorizeAsync(...dsAuthorizationBroker.cs以上就是C#学习教程:Howtosetreturn_uriforGoogleWebAuthorizationBroker.AuthorizeAsync?分享的所有内容,如果对大家有用需要进一步了解C#学习教程,我希望你多加注意——使用System;使用System.Collections.Generic;使用System.Threading;使用System.Threading.Tasks;使用Google.Apis.Auth.OAuth2;使用Google.Apis.Auth.OAuth2。流程;Apis.Auth.OAuth2.Requests;使用Google.Apis.Util.Store;命名空间OAuth2{公共类dsAuthorizationBroker:GoogleWebAuthorizationBroker{公共静态字符串RedirectUri;公共新静态异步任务AuthorizeAsync(ClientSecretsclientSecrets,IEnumerable范围,字符串用户,CancellationTokentaskCancellationToken,IDataStoredataStore=null){varinitializer=newGoogleAuthorizationCodeFlow.Initializer{ClientSecrets=clientSecrets,};返回等待AuthorizeAsyncCore(初始化程序、范围、用户、taskCancellationToken、dataStore)。ConfigureAwait(假);}privatestaticasyncTaskAuthorizeAsyncCore(GoogleAuthorizationCodeFlow.Initializerinitializer,IEnumerablescopes,stringuser,CancellationTokentaskCancellationToken,IDataStoredataStore){initializer.Scopes=scopes;initializer.DataStore=dataStore??新文件数据存储(文件夹);varflow=newdsAuthorizationCodeFlow(initializer);返回awaitnewAuthorizationCodeInstalledApp(flow,newLocalServerCodeReceiver()).AuthorizeAsync(user,taskCancellationToken).ConfigureAwait(false);}}publicclassdsAuthorizationCodeFlow:GoogleAuthorizationCodeFlow{publicdsAuthorizationCodeFlow(Initializerinitializer):base(initializer){}publicoverrideAuthorizationCodeRequestUrlCreateAuthorizationCodeRequest(字符串重定向ctUri){返回base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
