0
votes

I am trying to authenticate my web application with google oauth2. I am using vb.net as code behind.

At first step I added a hyperlink which redirect to https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=%2Fprofile&redirect_uri=http%3A%2F%2Flocalhost:2690%2Ftest1.aspx&response_type=code&client_id=XXX.apps.googleusercontent.com

Now after receiving code from code, I used code on page load of test1.aspx

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles    Me.Load
    If Not IsNullOrEmpty(Request.QueryString("code")) Then
                    Dim buffer As Byte() = Encoding.UTF8.GetBytes("code=" + Request.QueryString("code") + "&client_id=XXX.apps.googleusercontent.com&client_secret=XXX&redirect_uri=https%3A%2F%2Flocalhost:2690%2Ftest1.aspx&grant_type=authorization_code")     
        Dim req As HttpWebRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token")
        req.Method = "POST"
        req.ContentType = "application/x-www-form-urlencoded"
        req.ContentLength = buffer.Length

        Dim strm As Stream = req.GetRequestStream()
        strm.Write(buffer, 0, buffer.Length)
        strm.Close()
        Try
            Dim res As HttpWebResponse = req.GetResponse()
            Response.Write(res.StatusDescription)
        Catch wex As WebException
            Response.Write(wex.Data.ToString + "<br/>" + wex.InnerException.ToString + "<br/>" + wex.Message + "<br/>" + wex.TargetSite.ToString)
        End Try


    End If
End Sub

Each time I am getting bad request error from server. Please help me to find out what I am doing wrong. I also tried to use dotnetopenauth but each example is using mvc and c# and I only know vb.net. Thanks for your help….

1

1 Answers

0
votes

Is this code URLencoding the values in the body of the POST request.

Also keep in mind that the code is to be exchanged once for tokens.