[英]Access Google Drive Files from C# Web Application
您好,我在访问 Google Drive SDK 时遇到了一些问题,主要是身份验证步骤。
我的 .Net Web 应用程序中有两个主要页面 - Default.aspx.cs
和WebForm1.aspx.cs
。 在Default.aspx
我有一个hyperlink
控件,当用户单击链接时,它会将用户带到Google authentication page
:
https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&redirect_uri=http://localhost/GoogleDriveTest/GoogleDriveTest/GoogleDriveTest/WebForm1.aspx&state=/profile&client_id=*CLIENT_ID*&approval_prompt=force
一旦用户被重定向回REDIRECT_URI
( WebForm1
),我使用这段代码来访问authorization
代码:
HttpRequestInfo request = new HttpRequestInfo(Request);
code = Request.QueryString["code"];
现在我被困住了。 我知道我需要现在POST
此代码:
https://accounts.google.com/o/oauth2/token <insert POST parameters here>
但我完全不知道如何做到这一点。 我尝试了很多东西,但我得到的只是:
Server time out error - it failed to connect to the requested server
我该如何解决这个问题?
编辑 24/09/2012:
随着 Visual Studio 2012 的新版本,他们已经合并了 OAuth,因此它负责身份验证: http : //www.asp.net/vnext/overview/videos/oauth-in-the-default-aspnet-45-templates
这意味着用户可以使用 Google 等外部帐户登录本地 Web 应用程序。
这是否意味着,一旦用户通过 Google 登录,我就可以获取所需的 Google Drive 文件? 或者这只是为了使本地 Web 应用程序中的注册处理更容易?
再次感谢。
这是我的错误:
[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [2404:6800:4008:c01::54]:443]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +251
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +279
[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) +6098637
System.Net.HttpWebRequest.GetRequestStream() +13
GoogleDriveTest.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Projects\GoogleDriveTest\GoogleDriveTest\GoogleDriveTest\WebForm1.aspx.cs:101
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
解决了!!
我的问题是我的工作场所中有一个代理,不允许连接到所需的 URL。 所以我在我的浏览器中关闭了代理,它绕过它直接通过所需的 URL 成功获取访问令牌:D
请参阅快速入门(注意 .NET 选项卡!),其中显示了如何使用 DotNetOpenAuth 库执行此操作。
请注意以下行:
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
该文档包含一个用 C# 编写的完整 Web 应用程序示例,您可以将其用作参考:
https://developers.google.com/drive/examples/dotnet
它是一个 ASP.NET MVC 应用程序,但授权部分可以轻松地在 Web 窗体应用程序中重用。
有关授权流程的更多详细信息,请查看有关如何检索和使用 OAuth 2.0 凭据的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.