Zach King
Zach King

Reputation: 1208

How to Upload a Photo to Facebook with Python 3

Okay so I have tried everything I could possibly find on the internet and forums, but nothing works. Many of the modules like the facebook-sdk and such have syntax errors due to them being built for python 2.X while I am on python 3 (and I intend on keeping it that way). My best bet so far is with fbconsole. I have can post simple status updates and such easily, but I can't seem to upload a photo!? One question concerning this is: What do I need to put in fbconsole.OAUTH_SCOPE = [] to allow me to authenticate the whole photo-upload jazz? My urllib, urllib2, and urllib.request.urlretrieve() stuff all throws errors too...

If anyone can solve this for me, I will gladly make a video or text tutorial for it. I will do the same if I figure it out sooner. So good luck!

This is ONE of my errors:

C:\Users\Zach\Downloads\facebook-sdk-master\facebook-sdk-master>python
Python 3.3.1 (v3.3.1:d9893d13c628, Apr  6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import fbconsole
>>> fbconsole.OAUTH_SCOPE = ['publish_stream', 'read_stream']
>>> fbconsole.authenticate()
127.0.0.1 - - [30/Dec/2013 20:36:54] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Dec/2013 20:36:54] "GET /?access_token=CAACjeiZB6FgIBAGkN1XsOxC4gtJ2ukeiZBnJr9sViG
QZBsV63yJFmVohvhz5meYUbMJAXWy4ZBNNsYcXoD4BgMqTjSuPP8n5U0uN8exqSZAOlUeMoYtDWElAZCQy9WMiDgNcRSVKz94oZC
qwh4uwaXPmZBuZBrgeENHCtkD5ZCWPvph9CIfZBpI9NcG7WSFeZBEdGeYZD&expires_in=4987 HTTP/1.1" 200 -
>>>
>>> fbconsole.post('/me/photos', {'message':'Test Picture', 'source':open('screenshot.jpg')})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 763, in post
  File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 673, in post
  File "C:\Python33\lib\urllib\request.py", line 471, in open
    req = meth(req)
  File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 142, in http_reque
st
  File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 168, in multipart_
encode
  File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 111: character maps to <undef
ined>
>>>

Upvotes: 2

Views: 4589

Answers (2)

akimul
akimul

Reputation: 329

You can achieve this using python facebook sdk. First install facebook-sdk: pip install facebook-sdk

Then use the following code blocks:

import facebook
import os
import sys
import io
from PIL import Image
def main():
    graph = facebook.GraphAPI(access_token='Your Access token', version='2.6')
    graph.put_photo(image=open("path to your image file", 'rb'),message='Caption of the image', profile_id='profile/page id')

if __name__ == "__main__":
    main()

While generating access token, check the following permissions: manage_pages, publish_actions, publish_pages

Upvotes: 0

Tuan Chau
Tuan Chau

Reputation: 1333

I guess your code is:

file = open('image.jpg', 'r')

Let change to this:

file = open('image.jpg', 'rb')

You may get bug from facebook python sdk. Something like TypeError: sequence item 0: expected str instance, bytes found

I have fixed several bugs and upload here:

https://github.com/tuanchauict/facebook-sdk-python3

You can try.

Demo:

token = 'get_your_user_token_or_from_Graph_API_Explorer'
graph = fb.GraphAPI(token)

file = open('test.jpg', 'rb')

graph.put_photo(file, "this is a test photo")

Upvotes: 1

Related Questions