facebook Access Token 400 bad request
Asked Answered
F

3

8

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?

Fagoting answered 29/5, 2012 at 12:33 Comment(2)
Try this: #8241126Hospitality
@Thomas, unfortunately not same problemFagoting
U
4
  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/

Underhung answered 30/5, 2012 at 9:58 Comment(2)
Wow, I had the same issue (no trailing slash). Hard to believe that you get an error response 400 Bad Request when that is omitted.Euh
The absence of a trailing slash did not effect me.Mukden
L
1

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.

Lavalava answered 29/5, 2012 at 15:56 Comment(3)
I did that in earlier code already. the 'code' part is working. as I said the 400 bad request happens when I requesting for authentication tokenFagoting
The error says there is something wrong with the code, so make sure it isn't being modified or altered in anyway. It might be the cause of the problem.Lavalava
+1 to recover from the downvote. Even though this did not solve the problem, it still doesn't deserve a downvote. It may help others.Heteropterous
Z
1

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.

Zooplankton answered 11/1, 2016 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.