Facebook Graph API是Facebook提供的官方接口,允许开发者通过编程方式与Facebook平台交互。360°全景图是一种特殊类型的图片,允许用户在360度范围内环视场景。
首先需要:
publish_pages
权限// 使用FormData构建请求
const formData = new FormData();
formData.append('access_token', 'YOUR_ACCESS_TOKEN');
formData.append('source', imageFile); // imageFile是File对象
formData.append('allow_spherical_photo', 'true'); // 关键参数,标记为360图片
// 发送POST请求
fetch('https://graph.facebook.com/v19.0/me/photos', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log('Photo uploaded:', data);
// 返回的data中包含photo_id
})
.catch(error => {
console.error('Error uploading photo:', error);
});
// 如果上传到页面而不是个人资料,使用page_id代替me
fetch(`https://graph.facebook.com/v19.0/me/feed`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
access_token: 'YOUR_ACCESS_TOKEN',
message: 'Check out this 360° panorama!',
attached_media: `[{"media_fbid":"${photoId}"}]` // 使用上一步返回的photo_id
})
})
.then(response => response.json())
.then(data => {
console.log('Post published:', data);
})
.catch(error => {
console.error('Error publishing post:', error);
});
publish_pages
权限(对页面)publish_to_groups
权限(对群组)publish_actions
权限(对个人资料)allow_spherical_photo=true
通过以上方法,你可以成功使用JavaScript Graph API在Facebook上发布360°全景图。
没有搜到相关的文章