gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:08:36 34 4
gpt4 key购买 nike

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

我尝试使用来自 this example 的代码在谷歌和 this example在 Daimto 以及一些有用的提示 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 端口,我想使用 /authorize 以外的页面在我的网站上。具体来说,我想在开发过程中使用 http://localhost:888/Pages/GoogleApiRedirect但除了我在开发人员控制台中所做的之外,我不知道在哪里设置它。

如何显式设置 redirect_uri 的值?我也愿意接受“这种方法是完全错误的”形式的回应。

编辑:

在过去一天玩过之后,我发现通过使用 native 应用程序的客户端 ID/客户端密码而不是 Web 应用程序,我至少可以访问 Google 的 Web 授权页面而不会提示redirect_uri_mismatch。这仍然是 Not Acceptable ,因为它仍然返回到http://localhost:<some-random-port>/authorize。 ,这是我的网络应用程序无法控制的。

最佳答案

您可以使用此代码:(来自 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, scopes, user,
taskCancellationToken, dataStore).ConfigureAwait(false);
}

private static async Task<UserCredential> AuthorizeAsyncCore(
GoogleAuthorizationCodeFlow.Initializer initializer,
IEnumerable<string> scopes,
string user,
CancellationToken taskCancellationToken,
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);
}
}
}

关于c# - 如何为 GoogleWebAuthorizationBroker.AuthorizeAsync 设置 return_uri?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209273/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com