Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.63 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Facebook Access Tokens
  2. function getAccessToken(){
  3.         $app_id = FB_APP_ID;
  4.         $app_secret = FB_SECRET_ID;
  5.         $my_url = FB_PAGE_URL;
  6.         $code = $_REQUEST["code"];  
  7.  
  8.  
  9.         if(empty($code)) {
  10.        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
  11.        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
  12.          . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
  13.          . $_SESSION['state'];
  14.  
  15.        echo("<script> top.location.href='" . $dialog_url . "'</script>");
  16.      }      
  17.     if($_REQUEST['state'] == $_SESSION['state']) {
  18.        $token_url = "https://graph.facebook.com/oauth/access_token?"
  19.          . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
  20.          . "&client_secret=" . $app_secret . "&code=" . $code;
  21.  
  22.        $response = file_get_contents($token_url);
  23.        $params = null;
  24.        parse_str($response, $params);
  25.  
  26.        $graph_url = "https://graph.facebook.com/me?access_token="
  27.          . $params['access_token'];
  28.  
  29.        $user = json_decode(file_get_contents($graph_url));
  30.        echo("Hello " . $user->name);
  31.      }
  32.      else {
  33.        echo("The state does not match. You may be a victim of CSRF.");
  34.      }
  35. }
  36.        
  37. Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?
  38. client_id=ID HERE&redirect_uri=URLHERE&cli
  39. ent_secret=SECRECT&code=AQCI5rNgw9zCPHWGozeT59asg7_022u5tVc5XSef49BiX
  40. IaF5_MAMqFwsqOAquUHgjOu_99ONwUV6IC7k-jV6DsWf9ni3jm8t59aHCBp1jrFaDthPbIKLNLQ-
  41. fZgB5MLh1le5BAPKj_l57jhTLTBfOdxRU30mFCMYzMch8MYFpCmJ9GrjSSGwt0OKb_LNqMoRf8) [function.file-
  42. get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request