2016年4月6日 星期三

FaceBook oAuth for WebSite

Main Url : https://developers.facebook.com/apps 

1. 先進行註冊後即可於個人FB左下角看到開發人員項目,點選"管理應用程式"

2. 建立新的FaceBook APP取得App ID


3. APP Dashboard 

   a.來取得 App Secret,程式呼叫時需要帶入
   b. 在Settings頁設定該APP相關資訊 
   c. 在App Review設定Public為Yes <- 請先檢查相關設定均已完成。

<?php
  $tokenurl='https://graph.facebook.com/oauth/access_token';
  $client_id='170145......';
  $redirect_uri='http://www.xxx.net/zz.php';
  $client_secret="6f5cbdea2bc......";

  //取得Login的Access_token
  $tokenurl=$tokenurl."?client_id=".$client_id."&redirect_uri=".$redirect_uri."&client_secret=".$client_secret."&code=".$_GET["code"]; 
  $xml = file_get_contents($tokenurl);

 $NewString = preg_split("/[&,]+/", $xml);
 $appsecret_proof = hash_hmac('sha256', $NewString[0], $client_secret);

  //取得Login_id 
  $GetUserUrl='https://graph.facebook.com/me?access_token='.$access_token.'&appsecret_proof='.$appsecret_proof;  

  $res = file_get_contents($GetUserUrl);  
  $arr = json_decode($res);

  echo $arr->id. $arr->name; 

?>

沒有留言: