4

I'm trying to promote my app using app center but from the Web preview I can't visit the web site. The link that is called is http://www.myappsite.it/?fb_source=appcenter&fb_appcenter=1&code=a_long_string

From the index.php of myappsite I use this peace of php code to get the user coming from facebook

$code = $_REQUEST["code"];

if($_REQUEST['state'] == $_SESSION['state'] && !$user && strlen($code) > 0) 
{
$token_url = "https://graph.facebook.com/oauth/access_token?" . 
"client_id=" . $appId . "&redirect_uri=" . urlencode("http://www.myappsite.it/") . 
"&client_secret=" . $secret . "&code=" . $code;

$response = @file_get_contents($token_url);
$params = null;

parse_str($response, $params);

$graph_url = "https://graph.facebook.com/me?access_token=" .     $params['access_token'];

$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}

but $params['access_token'] is empty because the $token_url returns

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

how can I get the user logged in from tha app center preview web page?

Igy
  • 43,710
  • 8
  • 89
  • 115

2 Answers2

6

From the Authenticated Referrals part of the docs (the App centre counts as an authenticated referral)

See A special consideration when using the Query String setting:

If you would like to use the server-side authentication flow it is important to make sure you are passing the redirect_uri parameter correctly when exchanging your code for an access token. You should set your redirect_uri parameter to the click-through URL to your site without the code parameter. In most cases the URL will look something like:

http://www.example.com/?fb_source=search&code=CODE_HERE Once you strip the code parameter it will become:

http://www.example.com/?fb_source=search which is the value that should be set in redirect_uri. Please make sure that this logic is dynamic as the query parameters appended to your click-through URL may be subject to change.

Using your example from above, this means your redirect_uri parameter should be http://www.myappsite.it/?fb_source=appcenter&fb_appcenter=1

Igy
  • 43,710
  • 8
  • 89
  • 115
  • thanks that helped with the app center profile, however if i'm to click on any link from an activity on my facebook profile i keep getting the same error no mater what redirect_uri i use – Nour Wolf Jul 30 '12 at 10:19
0

As Igy said:

http://www.example.com/?fb_source=search&code=CODE_HERE Once you strip the code parameter it will become:

http://www.example.com/?fb_source=search

However if you are redirecting with this url it will need to be url encoded otherwise the ? will stop the redirect parameter and add the fb_source to your request.

It should look like this when sent out:

http%3a%2f%2fwww.example.com%2f%3ffb_source%3dappcenter%26fb_appcenter%3d1

(Which is what Marco had already done which is why he is now happy that it works)

urlencode("http://www.myappsite.it/")
Doolali
  • 956
  • 9
  • 13