Facebook oauth2 登录返回 (400) 错误请求



我正在将我的C#ASP.NET MVC 4 facebook登录转换为VB ASP.NET MVC 2版本项目。

不幸的是,我

收到此错误,我无法弄清楚问题是什么。

主要错误:错误请求 400 错误。更详细的错误:

WWW-身份验证:OAuth "Facebook 平台"invalid_code"验证验证码时出错。请确保您的redirect_uri与您在 OAuth 对话框请求中使用的相同"

控制器:

<HttpPost> _
    <AllowAnonymous> _
    Public Function ExternalLogin(provider As String, returnUrl As String) As ActionResult
        Return New ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", New With { _
            Key .ReturnUrl = returnUrl _
        }))
    End Function

    Public Function ExternalLoginCallback(returnUrl As String) As ActionResult
        Dim result As AuthenticationResult = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", New With { _
            Key .ReturnUrl = returnUrl _
        }))
        ' Eind resultaat krijg je hier
        If result.IsSuccessful Then
            ' Temporary Properties
            Dim strTempGeslacht As String = result.ExtraData("gender")

            ' Properties
            Dim strVoornaam As String = result.ExtraData("firstname")
            Dim strAchternaam As String = result.ExtraData("lastname")
            Dim strEmail As String = result.ExtraData("email")
            Dim strGeboortePlaats As String = result.ExtraData("hometown")
            Dim strWoonplaats As String = result.ExtraData("location")
            Dim arrayWerk As String() = result.ExtraData("work").Split("*"C)
            Dim intGeslacht As Integer = If(strTempGeslacht = "male", 0, 1)
            Dim intOpenID As Int64 = Int64.Parse(result.ProviderUserId)
            Dim CiDutch As New CultureInfo("nl-NL", False)
            Dim strBirthday As String = String.Format("{0:dd/MM/yyyy}", result.ExtraData("birthday"))

            ' Redirect
            Return RedirectToAction("Facebook", "Bedankt")
        End If
        ' Error
        Return RedirectToAction("Index", "Home")
    End Function

    Friend Class ExternalLoginResult
        Inherits ActionResult
        Public Sub New(provider__1 As String, returnUrl__2 As String)
            Provider = provider__1
            ReturnUrl = "http://localhost:25806/Facebook/ExternalLoginCallback"
        End Sub
        Public Provider As String
        Public ReturnUrl As String
        Public Overrides Sub ExecuteResult(context As ControllerContext)
            OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl)
        End Sub
    End Class
End Class

查询访问令牌(使用下载字符串时发生错误)

 protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
        {
            WebClient client = new WebClient();
            string url = string.Format("{0}?client_id={1}&client_secret={2}&redirect_uri={3}&code={4}", TokenEP, this._appId, this._appSecret, HttpUtility.UrlEncode(returnUrl.ToString()), authorizationCode);
            string content = client.DownloadString(url);
            NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(content);
            if (nameValueCollection != null)
            {
                string result = nameValueCollection["access_token"];
                return result;
            }
            return null;
        }

查询网址:

"https://graph.facebook.com/oauth/access_token?client_id=607648425938521&client_secret=ff099b80cc19da0ec0870df99fa465b0&redirect_uri=http%3a%2f%2flocalhost%3a18774%2fvacatures%2fExternalLoginCallBack%3f__provider__%3dfacebook%26__sid__%3d3dda4b1db178404094cbeef339d414b9&code=AQAAHJ0UoPjqvtOLjgUJ7ipzY0j7-8FxoTx7nt_Vxq9FD0cN-DhVH8BclofFEXPsPU7Fm1YHKGSlJRcLGedHSA23sVpkbssBp5yQo3PmBkUSZ9LBuckKrkjagvz4HkgFQ_oX2DoEPDmpkKo8O97GYlBt7j185SncBcpmbi8I7DNs9Z8wGP_FGQb8Mh6iMz3SH4IB1Ae3OppthmRXEJOj9k7xiboPsAYTf3w7E6MhWU3uNnR-SfA5RkSXkLas7xUnKF7eqmPM9_3pAMj2ObOAA1e4dcKke-QABo_FGx7LV0OY1pEeKIbbu9Ag5h0SBqmGi9k

工作项目 C# mvc 4 中的所有链接都相同。

6 小时,我花了 6 个小时试图找出问题所在。

它只是一个大写的B,我做了第一个请求:ExternalLoginCallback和第二个请求:ExternalLoginCallBack

我既高兴又难过,哈哈!

我怀疑这实际上与 .NET 没有任何关系。在您的代码中,您有

ReturnUrl = "http://localhost:25806/Facebook/ExternalLoginCallback"

但是,您提供的查询 URL 中的值为

http://localhost:18774/vacatures/ExternalLoginCallBack

在 Facebook 应用程序的 facebook.com 设置中,有一个返回 URI 字段。此值可能与您在此处的值不同。您必须更改其中一个,以便它们匹配。这就是错误所解释的。

我以前遇到过这个问题。主要错误是,在redirect_uri中,我没有在最后添加/。如果应用程序处于测试模式并且Facebook说:

如果仅启用开发模式,则会自动解析所有重定向 http://localhost,无需在此处添加它们。

然后,您应该将/添加到redirect_uri(http://localhost:3000 /)。如果这是开发人员模式,请不要忘记将redirect_uri添加到"有效的 OAuth 重定向 URI"。请求中的redirect_uri必须与"有效 OAuth 重定向 URI"中的相同。

最新更新