Jump to content

Facebook OAuth issues.


Ninjakreborn

Recommended Posts

Been fighting with this for awhile.  Finally needed to seek advice. I have a URL setup to go to facebook and get information.

It's just a standard link...

a href="https://www.facebook.com/dialog/oauth?client_id=CLIENTIDHERE&redirect_uri=http://www.website.com/facebook_connect.php?response_type=token">Facebook Connect</a></p>

 

That ends up sending them back to my receiver script. I have it laid out as follows:

<?php
/* Facebook OAuth Procedures */
$app_id = APPIDHERE;
$app_secret = SECRETKEYHERE;

$code = $_REQUEST["code"];

$url = 'https://graph.facebook.com/me?access_token=' . $code;

$user = json_decode(file_get_contents($url));

echo("Hello " . $user->name);
?>

That should be very simple. The link takes them to facebook. This part works. If they are logged in and have approved it, it then takes them to my receiver URL. If they are not logged in, it prompts for login, or if they decline and then it takes them to a failure page at that point. 

 

So all of that works. Now this receiver page also receives the code back from the previous URL. That all works fine.  However, when I try to get to the graph to get the user

data, it returns "File Get Contents" and a bad request message.  That doesn't make any sense. According to the Facebook API Documentation this is suppose to be how you get

the data back. But in this situation I don't even need there name. The only data I need is the Facebook userid so I can match that up with my database and find a connection, then log them into my system.

 

Any advice on why this is not working?

Link to comment
Share on other sites

I've found, especially when using HTTPS, that using cURL is the answer. 

 

A lot of times there are SSL verification steps that I can't get to work with standard fopen() and file_get_contents() functions due to a back-and-forth between the server and client.  I find that I have a lot more control over the environment (including which headers I send, whether or not to verify an SSL cert, etc.) with cURL. 

Link to comment
Share on other sites

I normally would, but according to the Facebook API Docs, this is the way they intended for you to get access.  If I can't figure this out after a minute, I might try that.  This is what I have now, I got a step closer but still having issues.

 

I have gotten a little further. I realized I was missing a step (the APP Auth). So I redid the same thing.  However on that second set of code, I did a redirect instead to try and get the code.

 

So first I changed my code to this

<a href="https://www.facebook.com/dialog/oauth?client_id=162015613847432&redirect_uri=http://www.mywebsite.com/facebook_connect.php?response_type=token">Facebook Connect</a>

 

The facebook_connect.php was also altered.

/* Facebook OAuth Procedures */
$app_id = 'IDHERE';
$app_secret = "SECRETKEYHERE";

$keycode = $_REQUEST["code"];

$url = 'https://graph.facebook.com/oauth/access_token?client_id=' . $app_id . '&redirect_uri=http://www.mywebsite.com/facebook_connect2.php&client_secret=' . $app_secret . '&code=' . $keycode;

header('Location: ' . $url);

 

OK, so this is the step I was missing.  So this takes me to the page..I have another page called facebook_connect2.php. This is suppose to grab it, do the auth, and log them into my system.

<?php
echo '<pre>';
print_r($_REQUEST);
echo '</pre>';
?>

i haven't worked out this step yet, was just printing out the request variables for testing.  Now, when it redirects to that URL though it returns the following error:

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

 

How can I fix this.

Link to comment
Share on other sites

I'd try urlencoding your redirect_uri.  The slashes and all the formatting can play crazy games in the query string and is probably preventing the trailing variables from being understood.

 

$url = 'https://graph.facebook.com/oauth/access_token?client_id=' . $app_id . '&redirect_uri='.urlencode('http://www.mywebsite.com/facebook_connect2.php').'&client_secret=' . $app_secret . '&code=' . $keycode;
   
header('Location: ' . $url);

Link to comment
Share on other sites

I tried that, but for some reason I am still getting the same error.

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

That sounds like something related to my APP ID or my secret key.  So I went ahead and triple checked those.

The "Client ID" is my app id, and the "Client Secret" is my secret key. I have made sure all the information is correct, but it's

still returning the same error.  I did the encoding, but it didn't do anything.

 

Any more advice?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.