使用google oauth2对我的网络应用程序进行身份验证

时间:2013-01-12 06:39:36

标签: oauth oauth-2.0 google-oauth

我正在尝试使用google oauth2对我的网络应用程序进行身份验证。我使用vb.net作为代码。

首先,我添加了一个重定向到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

的超链接

现在从代码接收代码后,我在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

每次我从服务器收到错误的请求错误。请帮我弄清楚我做错了什么。我也尝试使用dotnetopenauth,但每个例子都使用mvc和c#,我只知道vb.net。 谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

此代码URL是否包含POST请求正文中的值。

另请注意,代码将被替换为令牌一次。