Search code examples
asp.netvb.netfacebookfacebook-graph-api

Facebook oauth2 login returns (400) bad request


I'm converting my C# ASP.NET MVC 4 facebook login to a VB ASP.NET MVC 2 version project.

Unfortunately i am getting this error which i can't figure out what the problem is.

The main error: Bad Request 400 error. More detailed error:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_code" "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request"

Controller:

<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

Query Acces Token (the error occurs here when using downloadstring)

 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;

        }

Query url:

"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

All the links are the same from the working project C# mvc 4.


Solution

  • 6 Hours, i spend 6 hours for trying to figure out what the problem was.

    It was just a capital B, I did the first request with: ExternalLoginCallback And the second with: ExternalLoginCallBack

    I'm happy and sad at the same time, haha!