2012-02-27 11 views
3

を使用しなければならない私は、コードを使用してFacebookのユーザー(現在のユーザー)U'mを取得しようとしている:アクティブなアクセストークンが

$app_id = "xxx"; 
$app_secret = "yyy"; 

//** Get user information 
//Create our application instance. 
$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $app_secret, 
)); 
// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 

     $user_profile = $facebook->api('/me');.... 

     // The $user_profile = $facebook->api('/me'); line throw an exception: 

     'message' => string 'An active access token must be used to query 
          'information about the current user.' (length=80) 

     'type' => string 'OAuthException' (length=14) 
     'code' => int 2500 

なぜ?

答えて

3

OAuth 2.0の認証/認可プロセスは、Authenticationに記載されています。

これを行う方法を説明する例がいくつかあります。私はPHPのSDKも使用していますが、クライアント側でJavaScriptを使用して認証を行うことを選択しましたが、これは簡単です。ただし、両方の方法については、ドキュメントで説明しています。

更新:私はこのコードをPHPとJavaScriptの組み合わせで使用しています。ここで正しく処理されないのは(AFAIK)、ユーザーがFacebookにログインしていないときにアプリケーションにアクセスしたとき、つまりFacebook経由ではなくURLを通じてアプリケーションに直接アクセスするときだけです。その場合は、通知やログインボタンの代わりに空白のページが表示されます。

とにかく、これは私が私のような成功(アプリケーションのメインページ)としてconfig.inc.phpと失敗ページ(ユーザーがパーマを付与するものではありませんでした)JavaScriptにからVARS合格した私のindex.php次のとおりです。

 <?php 
      require 'include/config.inc.php'; 

      //Check whether Facebook OAuth mechanism called back to this script with access_token or error 
      if (isset($_GET['expires_in']) && $_GET['expires_in']>0) 
      { 
       header('Location: '.$appname_canvasPage.$appname_successPage); 
       exit; 
      } 
      else if (isset($_GET['error'])) 
      { 
       //echo 'querystr: '.$_SERVER['QUERY_STRING']; 
       header('Location: '.$appname_canvasPage.$appname_failurePage); 
       exit; 
      } 
      else 
      { 
       require 'include/header_metadata.inc.html'; 
     ?> 
    </head> 

    <body> 
     <div id="fb-root"></div> 
     <script> 
      var appname_canvasURI = '<?php echo $appname_canvasURI; ?>'; 
      var appname_canvasPage = '<?php echo $appname_canvasPage; ?>'; 
      var appname_successPage = '<?php echo $appname_successPage; ?>'; 
      var appname_failurePage = '<?php echo $appname_failurePage; ?>'; 
      var appname_fbPerms = '<?php echo $appname_fbPerms; ?>'; 
      var appname_appid= '<?php echo $appname_appid; ?>'; 

      window.fbAsyncInit = function() 
      { 
       FB.init({ 
        appId  : appname_appid, // App ID 
        channelUrl : appname_canvasPage+'/channel.html', // Channel File 
        status  : true, // check login status 
        cookie  : true, // enable cookies to allow the server to access the session 
        oauth  : true, // enable OAuth 2.0 
        xfbml  : true // parse XFBML 
       }); 

       // Additional initialization code here 
       FB.getLoginStatus(function(response) 
       { 
        //console.log('getLoginStatus response: ',response); 
        if (response.authResponse) 
        { 
         //user is already logged in and connected 
         facebookCheckPerms(); // ensure all requires perms are available and if not request them 
        } 
        else 
        { 
         //app is not authorized or user is logged out 
         facebookOAuthRedirect(); 
        } 
       }); 
      }; 

      // Load the SDK Asynchronously 
      (function() 
      { 
       var e = document.createElement('script'); 
       e.type = 'text/javascript'; 
       e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
       //e.src = "http://static.ak.fbcdn.net/connect/en_US/core.debug.js"; 
       e.async = true; 
       document.getElementById('fb-root').appendChild(e); 
      }()); 

      function facebookCheckPerms() 
      { 
       var hasReqPerms=true; 
       FB.api(
       { 
        method: 'fql.query', 
        query: 'SELECT '+appname_fbPerms+' FROM permissions WHERE uid=me()' 
       }, 
       function(response) 
       { 
        for(var key in response[0]) 
        { 
         if(response[0][key]==0) 
         { 
          hasReqPerms=false; 
         } 
        } 
        if (hasReqPerms==false) 
        { 
         // user does not have required permissions, do OAuth 2.0 redirect to get permissions 
         facebookOAuthRedirect(); 
        } 
        else 
        { 
         // user has required permissions, start the app. 
         //console.log('checkperms: user has required permissions, start the app'); 
         top.location.href = appname_canvasPage+appname_successPage; 
        } 
       }); 
      } 

      function facebookOAuthRedirect() 
      { 
       var redirectURL = 'https://www.facebook.com/dialog/oauth/?client_id='+appname_appid+'&scope='+appname_fbPerms+'&redirect_uri='+encodeURIComponent(appname_canvasURI)+'&response_type=token'; 
       //console.log('redirectURL: '+redirectURL); 
       top.location.href = redirectURL; 
      } 

     </script> 
     <?php 
      } 
     ?> 
    </body> 
</html> 
+0

はあなたが持っていますか私のコードにどのような例が含まれていますか?しかし、$ user = $ facebook-> getUser(); nullではありませんか? –

+0

元の投稿を更新して例を追加しました... – Joni

関連する問題