【问题标题】:Authenticate my web application with google oauth2使用 google oauth2 验证我的 Web 应用程序
【发布时间】:2012-12-26 18:59:51
【问题描述】:

我正在尝试使用 google oauth2 对我的 Web 应用程序进行身份验证。我使用 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。 感谢您的帮助...。

【问题讨论】:

    标签: oauth oauth-2.0 google-oauth


    【解决方案1】:

    此代码是否对 POST 请求正文中的值进行 URL 编码。

    另外请记住,代码只能兑换一次代币。

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 1970-01-01
      • 2022-01-13
      • 2019-02-12
      • 2014-11-22
      • 2016-09-30
      • 2019-12-19
      • 2011-11-24
      • 1970-01-01
      相关资源
      最近更新 更多