我尝试在我的非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:与已注册的重定向URI不匹配”
我只能在我的Google开发者控制台证书页面注册这么多的重定向URI。我不想注册65535个端口,我想在我的网站上使用一个不同于/authorize
的页面。具体来说,我想在开发过程中使用http://localhost:888/Pages/GoogleApiRedirect
,但除了我在开发者控制台中所做的之外,我不知道在哪里设置它。
我如何显式地设置redirect_uri
的值?我也愿意接受“这种方法完全错误”的回答。
编辑:
在过去的一天里,我使用了这个方法,我发现通过使用Native Application的Client ID/Client Secret而不是Web Application,我至少可以访问Google的Web授权页面,而不会让它抱怨redirect_uri_mismatch。这仍然是不可接受的,因为它仍然会返回http://localhost:<some-random-port>/authorize
,这不受Web Application的控制。
4条答案
按热度按时间mlnl4t2r1#
您可以使用以下代码:(原始想法来自http://coderissues.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker)
dsAuthorizationBroker.cs
slwdgvem2#
如果您尝试在.NET应用程序非Web服务器应用程序(即C#控制台应用程序命令行程序)中使用GoogleWebAuthorizationBroker.AuthorizeAsync,则在创建Google OAuth配置文件(https://console.developers.google.com/apis)时,在凭据中选择以下选项至关重要。
桌面应用程序-现在也称为已安装的应用程序
它隐藏在“帮助我选择”新凭据下。您必须创建一个显示桌面应用程序而不是WebApp的新凭据。
单击“帮助我选择”
选择您想要的API库
即(Google日历API、YouTube)选择“用户数据”
第3步- OAUTH客户端ID -桌面应用程序!
是-无需授权文件”
即Javascript &重定向现在您有一个没有Web应用程序重定向授权的配置文件
使用“下载JSON”并将其保存到应用程序中
参考。当你查看这个文件时,你会注意到一组不同的参数,告诉代理这是一个应用程序。在这个例子中,我正在访问日历API的范围。只需将范围更改为你试图访问的任何API。**
若要在Docker中执行此程式
当你把这个部署到Docker时,你会得到一个异常,即操作系统不支持运行该进程。基本上,它试图在Metal Box上打开一个浏览器。这在Docker中是不可能的。
要解决这个问题,请修改代码,使用完整的绝对路径,如下所示
ca1c2owp3#
在创建oAuth客户端ID时选择“其他”帮助我解决了重定向问题。(使用“Web应用程序”选项会尝试重定向到带有随机端口的URL,这非常烦人)
现在我的Gmail API工作起来就像一个魅力:)
qhhrdooz4#
如果你正在努力构建一个控制台应用程序,能够通过Gmail的身份验证来发送邮件,下面是一个有效的方法。
LocalServerCodeReceiver
是监听从浏览器返回的访问令牌的关键。如果需要,这个代码还将获得一个刷新令牌,并将在文件系统中缓存令牌以供将来使用。注意事项: