Access Google Drive files from C # web application

Hi, I’m having problems accessing the Google Drive SDK, mainly the authentication steps.

I have two main pages in my .NET Web application - Default.aspx.cs and WebForm1.aspx.cs . In Default.aspx , I have a hyperlink control that takes the user to the Google authentication page when he clicks on the link:

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

As soon as the user is redirected back to REDIRECT_URI ( WebForm1 ), I use this piece of code to access authorization code:

 HttpRequestInfo request = new HttpRequestInfo(Request); code = Request.QueryString["code"]; 

Now i'm stuck. I know that I now need to POST this code:

 https://accounts.google.com/o/oauth2/token <insert POST parameters here> 

But I am completely stuck on how to do this. I tried a lot of things, but all I get:

 Server time out error - it failed to connect to the requested server 

How to solve this problem?


EDIT 09/24/2012:

In the new release of Visual Studio 2012, they included OAuth, so it performs authentication: http://www.asp.net/vnext/overview/videos/oauth-in-the-default-aspnet-45-templates

This means that the user can log into the local web application with an external account, such as Google.

Does this mean that after a user logs in through Google, I can capture the necessary Google Drive files? Or is it just to facilitate registration in a local web application?

Thanks again.


Here is my mistake:

 [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 

SOLVE!!

My problem was that there was a proxy server in my workplace that did not allow me to connect to the requested URL. Thus, I turned off the proxy server in my browser, and he circumvented it directly with the required URL, which successfully received the access token: D

+4
source share
2 answers

Refer to QuickStart (note the .NET tab!), Which shows how to do this with the DotNetOpenAuth library.

Pay attention to the following line:

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

The documentation includes a complete sample web application written in C #, which you can use as a reference:

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

This is an ASP.NET MVC application, but part of the authorization can be easily reused in the Web Forms application.

For more information about the authorization flow, see the documentation for Obtain and use OAuth 2.0 credentials.

0
source

Source: https://habr.com/ru/post/1434749/


All Articles