[SOLVED] 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”


<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:


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


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!

Answered By – Jamie

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *