[英]How to get publish_stream permissions for app to post in the wall of a Facebook Page
[英]Facebook post to my Page Wall without publish_stream permission
我有一個 web 應用程序,它允許人們直接注冊或通過 facebook 注冊插件使用他們的 facebook 憑據進行注冊。 我希望能夠將網站上發布的信息復制到我網站的 facebook 粉絲頁面。 到目前為止,如果我有一個未登錄到 facebook 的用戶,它將毫無問題地發布到我的頁面牆上,表明它來自我的網站應用程序。 但是,如果此人登錄到 facebook,但沒有授予我的應用程序 publish_stream 權限,我會收到權限錯誤。 如果我沒有獲得用戶的適當許可,是否可以簡單地強制帖子來自我的網站應用程序? 如果我的 facebook 頁面上的每個帖子都來自應用程序而不是來自用戶,我會很高興,但我根本找不到任何文檔顯示這是如何完成的,或者是否可以完成。
編輯
這是我正在使用的代碼,如果此人未登錄 facebook,我可以將其發布到我自己的牆上。 我已經授權該應用發布。
$attachment = array('message' => $notes,
'name' => 'Backcountryride.com',
'caption' => "Community Driven Transportation",
'link' => 'http://backcountryride.com',
'description' => $description,
'picture' => 'http://backcountryride.com/images/vanlogo.jpg'
);
$result = $facebook->api('/backcountryride/feed/','post',$attachment);
編輯2
這是我用來實例化 $facebook object 的代碼:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
謝謝!
您正在尋找的是“應用程序登錄”。 您可以為您的應用程序而不是用戶獲取 session。 不容易找到,幾周前我偶然發現了它。 然后,您只需使用您通常使用的功能,但使用您的“應用程序”session 而不是“用戶”session。
https://developers.facebook.com/docs/authentication/#app-login
這不再有效。 GO 這里, https://stackoverflow.com/a/11102147/704647
所以通過更多的研究,我能夠拼湊出一個解決這個問題的方法。 為了做到這一點,我需要使用圖表 API 和舊版 REST api 的組合。 在授予我的應用程序發布到我的牆上的權限后,我使用以下代碼將我的用戶在我的網站上的條目發布到我的牆上。
$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "XXXXXXXXXXXXXX";
$client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$token = curl_exec($curl);
curl_close($curl);
$message = rawurlencode($description.'. Notes:'.$notes);
$url = "https://api.facebook.com/method/stream.publish";
$postString = "message=$message&uid=XXXXXXXXXXXXX&$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
幾件值得注意的事情。 1. rest api $postString 中的 uid 是您的 facebook PAGE 的 uid。 您不需要 target_id,因為當 uid 屬於頁面時,它只能發布到自己。 2. 您的 $message 必須是 rawurlencoded,因為這是 rest api 接受的形式。 3.這將作為您的APP發布到您的頁面,而不是發布到您網站的用戶。 為此,您需要獲得所有用戶的 publish_stream 權限。 或者使用 ui 對話框在每個帖子中獲取它。 如果您只想將其發布到您的 OWN 頁面牆上,那么這兩種方法似乎都過大了。
因為我在很多地方都看到過這個問題的變體在stackoverflow上沒有得到回答(我在一個地方發布了這個答案),所以我認為值得在這里發布這個答案。
如果您不確定如何授予您的應用發布到 facebook 頁面牆的權限,請執行以下兩個步驟。 我從這篇文章中得到了這個
I first had to authorize offline access with this url (replacing 'MYAPIKEY'): http://www.facebook.com/login.php?api_key=MYAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/ connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access
Then, I needed to grant 'publish_stream' permissions to the PAGE with this url (replacing 'MYAPIKEY' and 'THEPAGEID'): http://www.facebook.com/connect/prompt_permissions.php?api_key=MYAPIKEY&v=1.0&next= http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=popup&ext_perm=publish_stream&enable_profile_selector=1&profile_selector_ids=THEPAGEID
希望這可以幫助某人。
我以前的解決方案不再有效,但這確實有效, https://stackoverflow.com/a/11102147/704647
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.