![](/img/trans.png)
[英]Facebook PHP SDK V5 - Get Page Access Token Not User Access Token
[英]Facebook PHP SDK V5 returns user access token as page access token
我正在嘗試使用facebook USER ACCESS TOKEN獲取PAGE ACCESS TOKEN我已完成的步驟是
我的應用程序路徑= http:// localhost / facebook_req /
<?php session_start(); include('Facebook/autoload.php'); $fb = new Facebook\\Facebook(array('app_id' => '{appid}', 'app_secret' => '{appsecret}', 'default_graph_version' => 'v2.7')); $helper = $fb->getRedirectLoginHelper(); $permissions = array('publish_pages','manage_pages','pages_show_list'); $loginUrl = $helper->getLoginUrl('http://localhost/facebook_req/callback.php', $permissions); echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
<?php session_start(); include('Facebook/autoload.php'); $fb = new Facebook\\Facebook(array('app_id' => '{appid}', 'app_secret' => '{appsecret}', 'default_graph_version' => 'v2.7')); $helper = $fb->getRedirectLoginHelper(); $permissions = array('publish_pages','manage_pages','pages_show_list'); $loginUrl = $helper->getLoginUrl('http://localhost/facebook_req/callback.php', $permissions); echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
2當我用facebook點擊登錄時,我得到了對話框,我允許我允許在callback.php文件中創建的應用程序,我使用了getRedirectLoginHelper(), 檢索/獲取了USER ACCESS TOKEN打印的用戶訪問令牌&token元數據,最后嘗試發布到用戶帳戶並且成功(請參閱下面的示例var_dump我的令牌元數據)
<code>
$_SESSION['fb_access_token'] = $accessToken->getValue();
D:\wamp64\www\facebook_req\callback.php:48:
object(Facebook\Authentication\AccessTokenMetadata)[13]
protected 'metadata' =>
array (size=7)
'app_id' => string 'xxxxxxxxxxxxxxx' (length=15)
'application' => string 'xxxxxxxxxxx' (length=8)
'expires_at' =>
object(DateTime)[17]
public 'date' => string '2016-11-03 11:35:38.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
'is_valid' => boolean true
'issued_at' =>
object(DateTime)[18]
public 'date' => string '2016-09-04 11:35:38.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
'scopes' =>
array (size=5)
0 => string 'manage_pages' (length=12)
1 => string 'publish_pages' (length=13)
2 => string 'pages_show_list' (length=15)
3 => string 'publish_actions' (length=15)
4 => string 'public_profile' (length=14)
'user_id' => string 'xxxxxxxxxxxxxxxx' (length=15)
</code>
ACCESS TOKEN - user access token received from step 2 callback.php file METHOD - GET FIELDS - nonupublic?fields=access_token This works on graph explorer
我的php代碼返回USER ACCESS TOKEN而不是PAGE ACCESS TOKEN
<code>
$fb = new Facebook\Facebook(....);
$requestx = $fb->request('GET', '/nonupublic?fields=access_token',array(),$_SESSION['fb_access_token']);
var_dump($_SESSION['fb_access_token']);
var_dump($requestx);
</code>
問題是var_dump($_SESSION['fb_access_token']);
和var_dump($requestx);
是相同的,他們沒有什么不同,我似乎無法發布到Facebook頁面
順便說一句,我按照文檔url上的指南 - https://developers.facebook.com/docs/pages/access-tokens
有誰可以請建議
正如@ceejayoz指出我的代碼工作我犯了一個錯誤我已經構建了請求但沒有在facebook php v5上執行它我需要執行請求
$response = $fb->getClient()->sendRequest($requestx);
一切都完美無瑕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.