使用 FB graph api 和 appengine-python SDK 创建相册的代码是什么?

发布于 2024-10-18 05:16:36 字数 776 浏览 1 评论 0原文

我在 PHP 中找到了以下代码..

python 中的等效代码是什么?

//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);

//Create an album
$album_details = array(
        'message'=> 'Album desc',
        'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);

//Get album ID of the album you've just created
$album_uid = $create_album['id'];

//Upload a photo to album of ID...
$photo_details = array(
    'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);

$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);

i found the follwing code in PHP ..

what is the equivalent code in python to do that ?

//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);

//Create an album
$album_details = array(
        'message'=> 'Album desc',
        'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);

//Get album ID of the album you've just created
$album_uid = $create_album['id'];

//Upload a photo to album of ID...
$photo_details = array(
    'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);

$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-10-25 05:16:36

正如 facebook 的人此处所写,他们将不再支持 python facebook sdk,因此最好制作通过本机 python 工具发出请求。

创建相册:

import urllib,urllib2
access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
path = "me/albums"
post_args = {'access_token':access_token,'name':"Test Album5", 'message':"Test Album 5"}
post_data  = urllib.urlencode(post_args)
file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" , post_data)
response = file.read() 

>>>response
'{"id":"XXXXXX702571"}'

上传图像:

我没有找到使用 urllib2 发送多部分/表单数据的简短方法,因此我使用了此答案中的示例 https://stackoverflow.com/a/6843405/592737

import pycurl
import cStringIO

url = 'https://graph.facebook.com/ALBUM_ID/photos'
file ='/path/to/img.jpg'

response = cStringIO.StringIO()
c = pycurl.Curl()
values = [
    ('file' , (c.FORM_FILE,  file)),
  ('access_token' , access_token),
  ('message' , 'Image Message'),
  ]


c.setopt(c.POST, 1)
c.setopt(c.URL,url)
c.setopt(c.HTTPPOST,  values)
#c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
c.close()

>>>response.getvalue()
{"id":"XXXXXX07961"}

但是如果你使用的是 facebook python-sdk 的某个分支(例如 https://github.com/pythonforfacebook/facebook-sdk)你可以用更短的方式做到这一点:

import facebook
access_token = "XXXXXXXXXXXXXXXXXXXXXXXX"
graph = facebook.GraphAPI(access_token)
resp = graph.put_object("me", "albums", name="Test Album",message="Test description")
graph.put_photo(open('/path/to/img.jpg'), 'Look at this cool photo!', resp['id'])
>>> _
{'id': '4394545113756'}

As facebook guys wrote here, they will no longer support python facebook sdk, so it better to make requests via native python tools.

Creating the album:

import urllib,urllib2
access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
path = "me/albums"
post_args = {'access_token':access_token,'name':"Test Album5", 'message':"Test Album 5"}
post_data  = urllib.urlencode(post_args)
file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" , post_data)
response = file.read() 

>>>response
'{"id":"XXXXXX702571"}'

Uploading image:

I didn't find a short way to send multipart/form data using urllib2, so I used example from this answer https://stackoverflow.com/a/6843405/592737

import pycurl
import cStringIO

url = 'https://graph.facebook.com/ALBUM_ID/photos'
file ='/path/to/img.jpg'

response = cStringIO.StringIO()
c = pycurl.Curl()
values = [
    ('file' , (c.FORM_FILE,  file)),
  ('access_token' , access_token),
  ('message' , 'Image Message'),
  ]


c.setopt(c.POST, 1)
c.setopt(c.URL,url)
c.setopt(c.HTTPPOST,  values)
#c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
c.close()

>>>response.getvalue()
{"id":"XXXXXX07961"}

But if you're using some fork of facebook python-sdk (like https://github.com/pythonforfacebook/facebook-sdk) you can do it shorter way:

import facebook
access_token = "XXXXXXXXXXXXXXXXXXXXXXXX"
graph = facebook.GraphAPI(access_token)
resp = graph.put_object("me", "albums", name="Test Album",message="Test description")
graph.put_photo(open('/path/to/img.jpg'), 'Look at this cool photo!', resp['id'])
>>> _
{'id': '4394545113756'}
为你鎻心 2024-10-25 05:16:36

Facebook 不支持,但您应该考虑 http://code.google.com/p/gae -simpleauth/ 用于 oauth 部分。

然后,正如其他答案所暗示的那样,使用像 urllib2 这样的 Python 库来进行图形调用(可能还有 simplejson 来解释响应)

Not supported by Facebook, but you should consider http://code.google.com/p/gae-simpleauth/ for the oauth piece.

Then, as other answers imply, use Python libs like urllib2 to make the graph calls (and possibly simplejson to interpret the responses)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文