如何为 GoogleWebAuthorizationBroker.AuthorizeAsync 设置 return_uri? [英] How do I set return_uri for GoogleWebAuthorizationBroker.AuthorizeAsync?
问题描述
我正在尝试在我的 Google Calendar API强>非 MVC .NET Web 应用程序.(这似乎是一个重要的区别.)
I am trying to use the Google Calendar API in my non-MVC .NET Web Application. (This appears to be an important distinction.)
我曾尝试使用 Google 的此示例中的代码和 这个例子 在 Daimto 以及一些有用的提示此处相关帖子的数量.
I’ve tried to use code from this example at Google and this example at Daimto along with some helpful hints from a number of 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:
.我不知道如何将其设置为其他内容,如 AuthorizeAsync
生成的以下示例 URL 所示:
Problem is, when Google’s OAuth2 page is called, redirect_uri
keeps getting set to http://localhost:<some-random-port>/authorize
. I have no idea how to set this to something else, as in the following example URL generated by AuthorizeAsync
:
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 错误页面并显示以下消息:
Google responds with a redirect_uri_mismatch error page with the message:
“请求中的重定向URI:http://localhost:XXXXX/authorize/ 没有匹配注册的重定向 URI”
“The redirect URI in the request: http://localhost:XXXXX/authorize/ did not match a registered redirect URI”
我只能在我的 Google 开发人员的控制台凭据页面中注册这么多重定向 URI.我不倾向于注册 65535 端口,我想在我的网站上使用 /authorize
以外的页面.具体来说,我想在开发过程中使用 http://localhost:888/Pages/GoogleApiRedirect
但除了我在开发人员中所做的之外,我不知道我将在哪里设置它.s 控制台.
I can only register so many Redirect URI’s in my Google Developer’s Console Credentials page. I’m not inclined to register 65535 ports, and I want to use a page other than /authorize
on my site. Specifically, I want to use, during development, http://localhost:888/Pages/GoogleApiRedirect
but have no clue as to where I would set this, beyond what I've done in the Developer’s Console.
如何显式设置redirect_uri
的值?我也愿意接受这种方法是完全错误的."形式的回应.
How do I explicitly set the value of redirect_uri
? I am also open to a response in the form “This approach is completely wrong.”
在过去一天玩这个之后,我发现通过使用本机应用程序而不是 Web 应用程序的客户端 ID/客户端密钥,我至少可以访问 Google 的 Web 授权页面而不会抱怨redirect_uri_mismatch.这仍然是不可接受的,因为它仍然返回到 http://localhost:<some-random-port>/authorize
,这是我的 Web 应用程序无法控制的.
After playing with this over the past day, I've discovered that by using the Client ID/Client Secret for the Native Application rather than the Web Application, I can at least get to Google's web authorization page without it complaining about a redirect_uri_mismatch. This is still unacceptable, because it still returns to http://localhost:<some-random-port>/authorize
, which is outside the control of my web application.
推荐答案
您可以使用此代码:(原始想法来自 http://coderissues.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker)
You can use this code: (original idea from 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
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);
}
}
}
这篇关于如何为 GoogleWebAuthorizationBroker.AuthorizeAsync 设置 return_uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!