When I do a simple web request to this URL using the the WebClient Object (or any standard HTTP request method using the .NET framework), I get a 400 Bad Request Error. When I tried with CURL, I get a valid response from the web server. It seems like Google doesn't like a windows request - something throws it off.
It works in https://developers.google.com/oauthplayground/ and it also works if I just create a web form and do a post - I get an access token back no problem.
But I do need to be able to do it programmatically. I also tried WebRequest and HTTP object but did not really have a luck working with them yet.
I found about 4-5 similar questions here and some of them have a code examples - I tried their code but had no luck. (Code that I have tried)
Function GetAccessToken(ByVal Code As String, ByVal sScopes As String) As ActionResult Dim sAccessURLTokenURL As String = "https://accounts.google.com/o/oauth2/token"
Dim oWebCLient As New WebClient
Dim oNameValueCollection As New NameValueCollection
Dim oResponse() As Byte
oWebCLient.Headers.Add("Content-type", "application/x-www-form-urlencoded; charset=utf-8")
oNameValueCollection.Add("client_id", "562344623411-b2mt2215qdfs34asdqwe345jmq2ec7su.apps.googleusercontent.com")
oNameValueCollection.Add("client_secret", "KjsPkBUTosdeROuVfkKBaAwm")
oNameValueCollection.Add("code", Code)
oNameValueCollection.Add("scopes", sScopes)
oNameValueCollection.Add("grant_type", "authorization_code")
oResponse = oWebCLient.UploadValues(sAccessURLTokenURL, oNameValueCollection)
End Function
What am I doing wrong here?
Update
Got it to work!
Will Clean Up The Code And Post It With Some Explanations