For the past 2 days I've been struggling trying to successfully authenticate users into my Facebook app. What I want to achieve in the end is to retrieve and store the access token of an user.
The problem I have comes by following the FB tutorial for authentication: http://developers.facebook.com/docs/authentication/
Just above the Client-side Flow section there is a snippet of code which should allow me to get the current user. Unfortunately, I only get an error:
Warning: file_get_contents(https://graph.facebook.com/me?access_token=) [function.file-get-contents]: failed to open stream: No route to host
Traced the error back to the point I can, and apparently there is an error on this line:
$response = #file_get_contents($token_url);
because if I echo the $response, I get nothing.
What is there not mentioned in the tutorial that I should be doing but I'm not because I have no idea I should do it?
Many thanks
You can achieve getting the access Token by
// Get the current access token
$access_token = $facebook->getAccessToken();
//And now echo the access token
echo $access_token;
Well, it got sorted in the end. Apparently it's a server problem. I tried developing the app on 2 different servers + locally (EasyPhp 5.3.8.1). On the 3rd server I tried it works for now (fingers crossed). Honestly, I have no idea why it's working now and why my local server didn't function as it should have. Well, it works for now :D
Related
On our server-side authentication with Facebook we get a random and weird issue. Facebook calls the call back URL two times with the same code. This is only happening for some users and not on every login.
This is the flow we have implemented on our side: https://developers.facebook.com/…/manually-build-a-login-fl… . We have been using it since the beginning of 2013 and we haven't noticed any issues so far.
And this is the error we get when we are exchanging the code for an access token the second time.
{"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100,"fbtrace_id":"traceID"}}
We tried to log this issue as a bug on https://developers.facebook.com/bugs but unfortunately it doesn't work.
It keeps showing unexpected error. Not to mention that i was unable to find the correct bug category.
Any idea on how we can fix this?
Thanks!
Could it be that users are clicking twice to process auth service? Try disabling the button before calling Facebook auth service
What language are you using?
I just had this problem in Ruby using Devise for Rails. I had created an omniauth.rb initializer file, and added a config line item in the devise.rb initializer file.
If you did this too, you can remove the omniauth.rb initializer file and you should be good to go!
I'm using the Facebook Javascript API for login in conjunction with the official Facebook PHP SDK on my server to execute the two following lines of code:
$helper = $fb->getJavaScriptHelper();
$accessToken = $helper->getAccessToken();
With the token, I'm further able to execute this code which gets the necessary details I need on the server:
$fb->setDefaultAccessToken($accessToken);
$response = $fb->get('/me?locale=en_US&fields=name,first_name,last_name,email,gender');
If I refresh the webpage I'm working with and let it fully load everything works correctly and I'm able to print to screen all of the details I get back in $response.
The problem I'm having, however, is that if I quickly refresh the screen (either by hitting F5 on Windows machines or COMMAND+R on Macs) before the Facebook javascript code executes I get the following thrown error from the Facebook API:
"This authorization code has been used"
How do I avoid this? Do I wrap the Facebook code on the client side in a jQuery document ready function? I hesitate to do that because I've been told that the Facebook Javascript code is good to go as a stand-alone script that is intelligent enough to know when the document is loaded.
I'm about ready to throw in the towel and just code a manual login process that totally bypasses the Facebook Javascript API. Thanks for your help.
Put the access code into a $_session['access_token'] and then redirect to another page to get the data.
Login Page
Callback Page (save into the session variable)
Working Page (work with the session variable)
See more: https://benmarshall.me/facebook-php-sdk/#example-login
I'm trying to use my generated App Access Token with requests to the Graph API, but for some unknown reason it simply will not work. I always get the dreaded Invalid OAuth access token signature error.
Now, let me clarify: I'm pretty sure I know what I'm doing. I did get it to work with a different FB app. I used the /oauth/access_token call to generate the App Access Token, and I can use that Token successfully in further graph calls for that FB app.
But when I try it for my new FB application, it fails. This is the Rails snippet:
app_access_token = URI.escape(ENV["FACEBOOK_APP_ACCESS_TOKEN"])
url = "https://graph.facebook.com/debug_token?input_token=#{user_access_token}&access_token=#{app_access_token}"
response = HTTParty.get(url)
I've also tried not-encoding the token; as expected, it doesn't work (and shouldn't).
I've double- and triple-checked my App ID and App Secret, and re-ran the generation (via curl), and I think it's all correct and copy/pasted without error. The only possibly-notable difference between the generated token of my old app and that of my new one is that the latter has a hyphen in it. Is that relevant?
I am currently working around this by using <App ID>|<App Secret> in place of the generated token, but I'd really like to get back to doing it the right way.
Argh! It turns out that I had a few extra characters that somehow got attached to the end of my generated token!
I don't know how those got there, but that was the problem. Just dumb old human error.
EDIT: Oh my god, every Facebook developer should know this page:
https://developers.facebook.com/tools/accesstoken/
Why isn't that linked from the docs about the access token? It would have saved me so much time and probably prevented my mistake!
I have been trying to subscribe to real-time updates for the past week and I have been unsuccessful.
When I do the HTTP post (using rest-client) to URL:
https://graph.facebook.com/<app-id>/subscriptions
...I get this error message as response:
{"error":{"message":"(#100) The parameter object is required","type":"OAuthException"}}
My header parameters were as follows:
access_token=***,
object=user,
fields=friends,
callback_url=https://aaa.appspot.com,
verify_token=(app secret key)
I'm not able to find where I'm going wrong.
Somebody please kindly guide/help me figure my mistake.
The below link was used as a reference.
http://www.fb-developers.info/tech/fb_dev/tutorial/bytopic/realtime_upd_02.php
Thanks you for your time in advance.
You need to send your request to https://graph.facebook.com/appid/subscriptions and not https://graph.facebook.com/subscriptions - although that might be an error in your question and not your code!
EDIT: I just checked the markup for your question and you do have appid in the URL, it just wasn't displaying properly
How are you getting the access token? You must use an application access token and not a user access token, i.e. retrieve the access token from https://graph.facebook.com/oauth/access_token?client_id=<APP_ID>&client_secret=<APP_SECRET>&grant_type=client_credentials
https://developers.facebook.com/docs/reference/api/realtime/ is the official documentation, that might be more up to date than the link you've been using.
This question was asked already, but the accepted answer is not helpful as the poster seems to have figured it out themself and left out how they got the access token.
Posting on Facebook wall from Codeigniter app
Anyhow, it seems that I can redirect to the login url and log in , but its not finding the auth token. Throwing this error:
Fatal error: Uncaught OAuthException: An active access token must be
used to query information about the current user. thrown in
/home/howekevin/biologicalfinder.com/dev/application/libraries/base_facebook.php
on line 1040
Also, if you use the example on the SDK and var_dump $user, I'm getting int 0 even after I'm logged in. I notice it's using a lot of $GET array stuff, and CodeIgniter doesn't jive with that. Has anyone solved this issue?
Ahh, so you found Elliot's broken code. Give my oauth library a spin:
http://getsparks.org/packages/codeigniter-oauth/versions/HEAD/show
Or Phil Sturgeon's:
http://getsparks.org/packages/oauth2/versions/HEAD/show
Our code should actually work ; )
Mind you mine and Phil's libraries only handle auth, but seems that's what you're having trouble with at the moment (and it's the hardest part). Check out the Facebook developer docs for the rest (if you can make a POST request, you can do this).