Postar em uma página específica


In [1]:
import facebook

In [2]:
access_token = 'EAACUzLmOZC7kBAODqMlhUdJXEtRJJoDWKJMCC32vC1cd9ou4FBrQNUT1pNxHJZBXZBmCl0dwSNPDl6w3YhjI6DLLwquy9AbWCZAlky2ZCiQRboCJkiuiITS8iZAZALVYZAlQP9d1W47s0j60rYeFk94ctCqrZAxST93KxGZBS5L4cRrgZDZD'

In [3]:
api = facebook.GraphAPI(access_token)

In [4]:
api.get_object('aulaspythonfia')


Out[4]:
{'id': '1625524341063166', 'name': 'Aulas Python - FIA'}

In [5]:
id_pagina = api.get_object('aulaspythonfia')['id']
print(id_pagina)


1625524341063166

In [6]:
api.put_object(id_pagina, "feed", message="Olá Mundo!")


Out[6]:
{'id': '1625524341063166_1761908567424742'}

In [7]:
api.put_object("aulaspythonfia", "feed", message="Hello, world")


Out[7]:
{'id': '1625524341063166_1761908744091391'}

Postando no feed da página


In [8]:
import simplejson as json

In [9]:
paginas = api.get_object('me/accounts')

In [10]:
decodificar = json.dumps(paginas, sort_keys=True, indent=4)

In [11]:
print(decodificar)


{
    "data": [
        {
            "access_token": "EAACUzLmOZC7kBANXdUbWPueNWi1TH4L6w7yimUsdyUNWzGVPH7JQFyZCZCZCZCRr5yDMCzqjl0DzrZCx6cwdKEwyh9NnYwz8tNtiwYOW0y0JhhMLqtjowwuZCyAL0twbNN8m8jsbgISRkMpY1VCbyOxYr9QZBieZBw9VUJRsntSago57VhTRmsOU4",
            "category": "Education",
            "id": "1625524341063166",
            "name": "Aulas Python - FIA",
            "perms": [
                "ADMINISTER",
                "EDIT_PROFILE",
                "CREATE_CONTENT",
                "MODERATE_CONTENT",
                "CREATE_ADS",
                "BASIC_ADMIN"
            ]
        },
        {
            "access_token": "EAACUzLmOZC7kBAAU6xjBg66PtRXqwsE6T6MUa06S3irNdMEzZCyDIaC6y1ne8ZA85Qr1bfncsw2UQB6RZAbgQrKkxlD5g0tq3ZBFTLrNFtgZAxCdIpCXBHgsZCS7v3ZBEBKRZAZAuYZCk3THXAD5bj4eXhm3xMpqbgHjufOQApDqJZAtXbr1tWjzjiin",
            "category": "Book Series",
            "id": "1675657189312530",
            "name": "Series sobre Livros legais",
            "perms": [
                "ADMINISTER",
                "EDIT_PROFILE",
                "CREATE_CONTENT",
                "MODERATE_CONTENT",
                "CREATE_ADS",
                "BASIC_ADMIN"
            ]
        }
    ],
    "paging": {
        "cursors": {
            "after": "MTY3NTY1NzE4OTMxMjUzMAZDZD",
            "before": "MTYyNTUyNDM0MTA2MzE2NgZDZD"
        }
    }
}

Agora precisamos iterar sobre os dados retornados e recuperar o token de acesso da página existente:


In [12]:
for pagina in paginas['data']:
    if pagina['id'] == id_pagina:
        pagina_token_acesso = pagina['access_token']

In [13]:
print(pagina_token_acesso)


EAACUzLmOZC7kBANXdUbWPueNWi1TH4L6w7yimUsdyUNWzGVPH7JQFyZCZCZCZCRr5yDMCzqjl0DzrZCx6cwdKEwyh9NnYwz8tNtiwYOW0y0JhhMLqtjowwuZCyAL0twbNN8m8jsbgISRkMpY1VCbyOxYr9QZBieZBw9VUJRsntSago57VhTRmsOU4

In [14]:
mensagem = 'Python Rocks!!!!'
post_pagina = facebook.GraphAPI(pagina_token_acesso)

In [15]:
post_pagina.put_wall_post(mensagem)


Out[15]:
{'id': '1625524341063166_1761908900758042'}

In [ ]: