質問

FacebookのJavaScript SDK "fb.ui"を使用して、OAuthダイアログを引き上げています。 [許可]をクリックしたら、セッションオブジェクトをキャプチャし、ユーザーIDを抽出し、スクリプトでさらに使用する必要があります。何らかの理由で、私はこれを適切に機能させることができません。セッションが存在していても、未定義になり続けています。

<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
    appId  : '***************',
    status : true,
    cookie : true,
    xfbml  : true
});
FB.getLoginStatus(function(response) {
    if (response.session) {
        //do something
    } else {
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            alert(response.session.uid); //returns underfined
        });
    }
});
</script>
役に立ちましたか?

解決 4

だから私はこれを私が望むように機能させました。これは最良の方法ではないかもしれませんが、多くの掘り下げて欲求不満の後、同じ質問をしている人が正しい軌道に乗るのに役立つことを願っています。

JSソース:

FB.getLoginStatus(function(response) {
    if (!response.session) {
        //initiate FB OAuth js popup dialog
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            if (response.session) { //if permission Allowed
                var thesession = response.session;
                var thesession = eval('(' + thesession + ')'); //decode json
                //POSTing to local file get.user_graph.php, to fetch user info
                $.ajax({
                    type: "POST",
                    url: "get.user_graph.php",
                    data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid,
                    dataType: "text",
                    success: function(user_graph){
                        var user_graph1 = eval('('+user_graph+')');
                        alert(user_graph1.name); //users name
                        alert(user_graph1.id); //users id
                        alert(user_graph1.email); //users email
                        alert(user_graph1.link); //users profile link
                    }
                });
            } else {
                //if permission Not Allowed
            }
        });
    }
});

get.user_graph.php出典:

//exchange our session for an access_token
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions');
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred');
$curl_token = curl_init(POSTURL);
curl_setopt($curl_token, CURLOPT_POST,1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS);
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl_token, CURLOPT_HEADER,0);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1);
$token = curl_exec($curl_token);

$token_decoded = json_decode($token,true);
//get the user graph (personal info)
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']);
echo $user_graph;

他のヒント

ユーザーIDを取得するには:

FB.getLoginStatus(function(response) {
  alert(response.authResponse.userID);
});

Facebookのログインボタンからログインすると、システムにCookieを保存します。これには、ユニークなアクセストークンが含まれています。そのCookieにはFacebook IDも含まれています。

応答を確認してください。私はそれが何かを言っているに違いない "display" must be one of "popup", "iframe" or "hidden". どうやらOAuthダイアログは、デフォルトの「ページ」ディスプレイでは呼び出せないようです。と display: 'iframe' Access_Tokenを含める必要があります。そのため、最初にOAuthダイアログを呼び出している>:l。

FB.UIは現在、OAUTHダイアログを取得するために使用できるとは思いません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top