問題描述
我希望用戶通過網站上的表單在 Facebook 頁面上發布圖片.當他們在本網站上通過 facebook 登錄后,他們可以從他們的電腦中選擇一張圖片.
I want users to post an image on a facebook page via a form on a website. When they have logged in via facebook on this website, they can select an image from their computer.
一旦他們選擇了圖像,我希望將其發布到用戶墻上,以及我是管理員之一的頁面的相冊中.
Once they have selected the image, I want it to be posted to the users wall, and in an album of the page where I'm one of the administrators.
我為此創建了一個應用程序,但我們似乎無法找到讓應用程序在此 facebook 頁面上發布的方法.
I have created an app for this, but we can't seem to find a way to get the app to post on this facebook-page.
我們需要為此頁面或應用設置任何權限嗎?
Do we need to set any permissions on this page or app?
推薦答案
要將圖像上傳到您是其管理員的 Facebook 頁面,您需要執行以下操作:
To upload images to a facebook page of which you're an admin you need to do the following:
1.) 創建一個 facebook 應用程序(通常的方式),確保你指定了 Canvas URL
1.) Create a facebook application (the usual way), make sure you specify the Canvas URL
2.) 導航到以下 url 以頁面管理員身份登錄,并授予權限 (user_photos,manage_pages,offline_access,publish_stream)
2.) Navigate to the url below logged in as the admin of the page, and give the permissions (user_photos,manage_pages,offline_access,publish_stream)
https://www.facebook.com/dialog/oauth?
client_id=<application_id>
&redirect_uri=<canvas_url>
&response_type=token
&scope=user_photos,manage_pages,offline_access,publish_stream
3.) 當您授予應用程序所需的權限時,您將被重定向到 canvas_url#access_token=*access_token*,例如
3.) When you give the application the required permissions you'll be redirected to canvas_url#access_token=*access_token*, for example
http://example.com/#access_token=awe12
4.) 然后導航到
https://graph.facebook.com/me/accounts?access_token=<access_token>
(使用 #3 中的訪問令牌).這將列出您管理的頁面;記下要上傳圖片的頁面的 access_token
(use the access token from #3). This will list the pages you administer; write down the access_token for the page(s) to which you want to upload the image
我不是 100% 確定,但我相信使用圖形 API 您只能將圖像上傳到通過圖形 API 創建的相冊;即您需要首先通過圖形 api 創建一個專輯.下面是使用 curl 的示例代碼:
I'm not 100% sure but I believe that using graph api you can upload images only to albums created via graph api; i.e. you need to first create an album via graph api. Here's sample code using curl:
$uri = sprintf(
'https://graph.facebook.com/%1$s/albums?access_token=%2$s',
$page_id,
$access_token
);
$post_fields = array(
'name' => trim( $album_name )
);
$curl = curl_init( $uri );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );
$raw_data = curl_exec( $curl );
curl_close( $curl );
$data = json_decode( $raw_data, $assoc = TRUE );
上面的 $data
將包含相冊 ID,您需要上傳照片:
The $data
above will contain the album id, which you'll need to upload a photo:
// prepare the curl post fields
$batch = sprintf(
'[{"method":"POST", "relative_url":"%1$s/photos", "attached_files":"file1"}]',
$album_id
);
$post_fields = array(
'batch' => $batch,
'access_token' => $access_token,
'file1' => '@' . $image_abs_path
);
$uri = 'https://graph.facebook.com';
$curl = curl_init( $uri );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );
$raw_data = curl_exec( $curl );
curl_close( $curl );
$data = json_decode( $raw_data, $assoc = TRUE );
這篇關于Facebook:通過 php 將圖像和描述發布到墻和頁面相冊中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!