Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook Access Token 400 bad request

I am using following code to retrieve facebook accessToken

string url = "https://graph.facebook.com/oauth/access_token?" +
                         "client_id={0}" +
                         "&redirect_uri={1}" +
                         "&client_secret={2}" +
                         "&code={3}";
            url = string.Format(url, clientId, redirectUri.EncodeUrl(), clientSecret, code);
            //Create a webrequest to perform the request against the Uri
            WebRequest request = WebRequest.Create(url);
            try
            {
                //read out the response as a utf-8 encoding and parse out the access_token
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        //string urlRedirects = response.ResponseUri.ToString();
                        Encoding encode = Encoding.GetEncoding("utf-8");
                        if (stream != null)
                        {
                            StreamReader streamReader = new StreamReader(stream, encode);
                            string accessToken = streamReader.ReadToEnd().Replace("access_token=", "");
                            streamReader.Close();
                            response.Close();
                            return accessToken;
                        }
                    }
                }
            }
            catch
            {
                return null;
            }

however I am constantly receiving this ambiguous error message

{
"error": {
"message": "Error validating verification code.",
"type": "OAuthException",
"code": 100
}
}

I checked the code 100 "Invalid parameter" doesn't means much to me at all.

anyone have had similar problem?

like image 623
D.J Avatar asked May 29 '12 12:05

D.J


3 Answers

  1. Check you are adding correct code in the url For example

    http://www.xyz.com/?code=AQC399oXame3UKmoAMYnqkZOEXPDNa8ZUFEY9sc6I4YNQnNT-ZgHzpMNnQVZrCUBZVqJRIB1QrXC5xW58_8MNIgQol_PaQvYssUM8OiKjSY5aoqGLBMuCeeHsSqP_mRTd1xiK0iretZcXwMm_27lFYrWFw345Mxod_lfJuB8zI13E8wJUQiArXW_ZlGLNcyxh20#_=_
    

Code must be

    code = AQC399oXame3UKmoAMYnqkZOEXPDNa8ZUFEY9sc6I4YNQnNT-ZgHzpMNnQVZrCUBZVqJRIB1QrXC5xW58_8MNIgQol_PaQvYssUM8OiKjSY5aoqGLBMuCeeHsSqP_mRTd1xiK0iretZcXwMm_27lFYrWFw345Mxod_lfJuB8zI13E8wJUQiArXW_ZlGLNcyxh20

code should not include following in the end

    #_=_ 

If above did not solve the problem


2. redirect_uri must end with /

redirect_uri=http://www.xyz.com/

The following gives some times above mentioned error

redirect_uri=http://www.xyz.com


3. A lso make sure App on Facebook and Website with Facebook Login are set with same addresss e.g http://www.xyz.com/

like image 60
maaz Avatar answered Oct 20 '22 05:10

maaz


You need to send the user to the Facebook Login page to get a valid code. The code should then be used to get the access_token for the user.

Follow the Authentication Guide.

like image 35
Niraj Shah Avatar answered Oct 20 '22 04:10

Niraj Shah


I also got error message 400, when my app id and secret were wrong (i had messed up develop and production id-s and secrets).

Fixing them (watch also out for the correct host) fixed this problem for me.

like image 1
Martin Sookael Avatar answered Oct 20 '22 05:10

Martin Sookael