Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Facebook Access Tokens
- function getAccessToken(){
- $app_id = FB_APP_ID;
- $app_secret = FB_SECRET_ID;
- $my_url = FB_PAGE_URL;
- $code = $_REQUEST["code"];
- if(empty($code)) {
- $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
- $dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
- . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
- . $_SESSION['state'];
- echo("<script> top.location.href='" . $dialog_url . "'</script>");
- }
- if($_REQUEST['state'] == $_SESSION['state']) {
- $token_url = "https://graph.facebook.com/oauth/access_token?"
- . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
- . "&client_secret=" . $app_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);
- }
- else {
- echo("The state does not match. You may be a victim of CSRF.");
- }
- }
- Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?
- client_id=ID HERE&redirect_uri=URLHERE&cli
- ent_secret=SECRECT&code=AQCI5rNgw9zCPHWGozeT59asg7_022u5tVc5XSef49BiX
- IaF5_MAMqFwsqOAquUHgjOu_99ONwUV6IC7k-jV6DsWf9ni3jm8t59aHCBp1jrFaDthPbIKLNLQ-
- fZgB5MLh1le5BAPKj_l57jhTLTBfOdxRU30mFCMYzMch8MYFpCmJ9GrjSSGwt0OKb_LNqMoRf8) [function.file-
- get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
Advertisement
Add Comment
Please, Sign In to add comment