即可以開始使用PHP撰寫 FB Oauth登入。
code部分如下:
<?php $app_id = "xxxxxxxxxxxxxxxxxxx"; //FB APP的 ID $app_secret = "xxxxxxxxxxxxxxxxxxx"; //FB APP的 Secret $my_url = "http://localhost:8000/fb.php"; // 驗證完畢來redirect的網址 session_start(); if(isset($_REQUEST["code"])) { $code = $_REQUEST["code"]; //返回的oauth 授權 返回code驗證資訊 } if(empty($code)) { $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection $dialog_url = "https://www.facebook.com/v2.12/dialog/oauth?client_id=".$app_id."&redirect_uri=".$my_url."&state=".$_SESSION['state'].'&scope=user_birthday,email,user_friends'."&response_type=code"; echo $dialog_url; echo "<br>"."<br>"; } if(isset($code)) { $token_url = "https://graph.facebook.com/v2.12/oauth/access_token?"."client_id=".$app_id."&redirect_uri=".$my_url."&client_secret=".$app_secret."&code=".$code; echo $code; echo "<br>"."<br>"; $curl = curl_init($token_url); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); echo var_dump($response); $response = json_decode($response); echo var_dump($response); echo "<br>"."<br>"; curl_close($curl); $graph_url = "https://graph.facebook.com/v3.0/me?access_token=" . $response->access_token."&fields=id,name,gender,friends"; $curl = curl_init($graph_url); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $user_data = curl_exec($curl); print_r($user_data); $user_data = json_decode($user_data); $user = $user_data->id; $url = "http://graph.facebook.com/".$user_data->id."/picture?type=large"; $name = $user_data->name; echo "使用者ID:".$user."</p><p>"; echo "使用者url:".$url."</p><p>"; echo "臉書名稱:".$name."</p><p>"; } else { echo("顯示錯誤,請從正確網址進入"); } ?> ?>
可以提供http://localhost/test/FB/fb_oauth.php測試嗎?
回覆刪除