2012-09-18 51 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

相關問題