sciyoshi / pyfacebook

PyFacebook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support upload of photos on other methods than photos.upload

joaopedrogoncalves opened this issue · comments

According to http://bugs.developers.facebook.com/show_bug.cgi?id=3961 , it is possible to send pictures by the same mechanism as photos.upload, as multipart data together with the api call.

This is already documented:
http://wiki.developers.facebook.com/index.php/Events.create

The PHP api checks for an extra parameter and calls a different API if present:

public function events_create($event_info, $file = null) {
if ($file) {
return $this->call_upload_method('facebook.events.create',
array('event_info' => $event_info),
$file,
Facebook::get_facebook_url('api-photo') . '/restserver.php');
} else {
return $this->call_method('facebook.events.create',
array('event_info' => $event_info));
}
}

import requests

def events_create(event_info, file=None):
    """
    Creates a new Facebook event.

    Args:
    - event_info: Dictionary containing information about the event.
    - file: Optional. File-like object containing the picture to be uploaded with the event.

    Returns:
    - Response from the Facebook API.
    """
    api_url = 'https://graph.facebook.com/v12.0/facebook.events.create'
    if file:
        files = {'file': file}
        response = requests.post(api_url, data=event_info, files=files)
    else:
        response = requests.post(api_url, data=event_info)
    return response.json()

# Example usage:
event_info = {
    'name': 'Sample Event',
    'start_time': '2024-12-31T12:00:00',
    'end_time': '2025-01-01T12:00:00',
    'location': 'New York City',
    'description': 'This is a sample event description.',
    # Add other event information as needed
}

# If you have a file object for the picture, pass it as 'file' parameter
# file = open('picture.jpg', 'rb')
# response = events_create(event_info, file=file)
# file.close()

# If you don't have a file object, omit the 'file' parameter
response = events_create(event_info)
print(response)