2012-09-18 53 views
4

嗨我在访问Google Drive SDK时遇到了一些问题,主要是验证步骤。从C#Web应用程序访问Google Drive文件

我的.Net Web应用程序中有两个主页 - Default.aspx.csWebForm1.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_URIWebForm1),我使用这段代码访问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等外部帐户登录本地网络应用程序。

这是否意味着,一旦用户通过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

+0

@Shaks感谢编辑是新来这个:) – apet083

+0

没有任何问题的文档。 :) –

回答

0

Refer to the QuickStart(注意.NET选项卡!)显示了如何使用DotNetOpenAuth库执行此操作。

注意以下行:

// Retrieve the access token by using the authorization code: 
return arg.ProcessUserAuthorization(authCode, state); 
+0

嗨bgever谢谢你的回复。我已经成功实施了Google提供的控制台应用程序示例。然而,我需要让它为Web应用程序工作,并且在读完一些内容后,我发现你不能使用'NativeApplicationClient',你需要使用'WebServerClient',它不支持你上面提到的方法 – apet083

0

文档包括C#编写的一个完整的Web应用程序示例,您可以参考使用:

https://developers.google.com/drive/examples/dotnet

这是一个ASP.NET MVC应用程序,但授权部分可以很容易地在Web窗体应用程序中重用。

有关授权流程的详细信息,请查看我如何Retrieve and Use OAuth 2.0 Credentials

+0

您好Claudio,谢谢你的回复。我已经查看了这两个链接并发现了一些问题。使用DrEdit,似乎我需要一个注册的域名,但我只使用本地主机。我读了一个关于更改主机文件或其他东西的线程,但似乎没有工作.. 同样与您的第二个链接,它似乎相当复杂 - 我只在开发人员模式下这样做,并不需要在数据库中存储凭证等 – apet083

+0

如果您的开发人员机器需要公共域名,您可以获得[免费的Dyn主机名](http://free.domain.name),将其指向您PC的公共IP地址(确保您的路由器上的NAT转发已正确完成),并将内部开发Web服务器的暴露情况更改为公开。 –

+0

好的,谢谢@bgever我会在下次尝试! – apet083

相关问题