Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I'm having the hardest time with Google APIs and I'd appreciate your help with this...

So basically I'm having trouble generating a GET System.Net.WebRequest to Google, that includes the access token for the purpose of obtaining the corresponding System.Net.WebResponse that'll include my user's profile data in JSON format. I'm trying to implement a C# Windows Form application that is able to Authenticate users and obtain their profile information (via OAuth2.0 authorization).

Here's an example of the code I've been trying:

C#
try 
{
// I'm already able to receive the Access Token from Google and store it so I just need to send a GET request with this token to receive the profile info:
RequestString = "https://www.googleapis.com/plus/v1/people/me?access_token=" + this.GoogleAccessToken;

#region ----- THIS IS ONE METHOD IVE TRIED for making the request -----
WebRequest GGLRequest = WebRequest.Create(RequestString);
GGLRequest.Method = "GET";
//tried with and without the next 3 lines with no difference:
GGLRequest.ContentLength = 0;
GGLRequest.ContentType = "application/json";
GGLRequest.Headers.Add("Authorization", "Bearer");

WebResponse GGLResponse = GGLRequest.GetResponse();
//<code continues for this method>
#endregion

#region ----- THIS IS ANOTHER METHOD WITH SAME RESULT -----
HttpWebRequest GGLRequest = (HttpWebRequest)WebRequest.Create(RequestString);
GGLRequest.Method = "GET";
//again, tried it with and without some or all the following lines
GGLRequest.Host = "www.googleapis.com";
GGLRequest.ContentType = "application/json";
GGLRequest.Headers.Add("Authorization", "Bearer");
GGLRequest.ContentLength = 0;
GGLRequest.UseDefaultCredentials = true;

HttpWebResponse GGLResponse = GGLRequest.GetResponse() as HttpWebResponse;
//<code continues for this other method>
#endregion               

On the .GetResponse line, the code always explodes with a Request Aborted failure (Could not create SSL/TLS secure channel).

Now, before anyone suggests me using the C# Client Library (Google.Apis.Auth.etc etc), I agree: It's way shorter and effective handling this; BUT I don't like it since I loose control of some of my app's flow. PLUS, the documentation is very very poor for novices like me.

Thanks for your time! :)
Posted

1 solution

Situation is resolved!

When you issue GET requests to Google (and I'm imagining any other company service like theirs), you have to perform it by using a secure line (SSL/TLS, etc.). Not doing so might trigger a number of errors depending on your specific scenario. In my case, the same code was giving me different error types on the .GetResponse line: one type at work and one type at home (apparently since I'm behind a company Actvie Directory at work). I mention this little detail because it was the factor that kind of made my light bulb turn on and start looking in a certain direction ;-)

Now, during previous research I had stumbled upon an article (I apologize for not including the link since I shamefully lost it) explaining that the WebRequest method automatically uses SSL when it "sees 'https'" as the protocol specified in the URL. Unfortunately, this is not enough.

You have to force the use of SSL/TLS in the code or otherwise the endpoint might not like your request.

Finally I stumbled upon This Article that gave me the light I needed. I simply had to add the following lines of code before issuing the WebRequest:

C#
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; // Tls for Facebook
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

It worked perfectly!

I hope this helps anyone with the same problem, in the future.

Have a great day! :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900