To post a image in facebook page using python

I am trying to post a image in fb using python script but it gives the following error raise GraphAPIError(result) facebook.GraphAPIError: Duplicate status message

import facebook

def main():
  # Fill in the values noted in previous steps here
  cfg = {
    "page_id"      : "value",  # Step 1
    "access_token" : "value"   # Step 3
    }

  api = get_api(cfg)
  msg = "Tasty and Healthy Curries"
  status=api.put_wall_post(msg)
  photo = open("http://www.us2guntur.com/html/htmlimages/us2gsweets_291114.jpg", "rb")
  graph.put_object("me", "photos", message="You can put a caption here", source=photo.read())
  photo.close()

def get_api(cfg):
  graph = facebook.GraphAPI(cfg['access_token'])
  # Get page token to post as the page. You can skip 
  # the following if you want to post as yourself. 
  resp = graph.get_object('me/accounts')
  page_access_token = None
  for page in resp['data']:
    if page['id'] == cfg['page_id']:
      page_access_token = page['access_token']
  graph = facebook.GraphAPI(page_access_token)
  return graph
  # You can also skip the above if you get a page token:
  # http://stackoverflow.com/questions/8231877/facebook-access-token-for-pages
  # and make that long-lived token as in Step 3

if __name__ == "__main__":
  main()

Upvotes: 1

Views: 1374

Answers (2)

akimul
akimul

Reputation: 329

Actually you can not post same status message over and over again using the API in small period of time and it is the error message from the API to avoid this type of scenario and spamming. Just change the message attribute on put_object method every time you post.

Upvotes: 1

Maciej Wilczyński
Maciej Wilczyński

Reputation: 377

There's probably nothing wrong with code you posted. The status message clearly tells you that the status update that you're trying to post is already shared on your profile. It's Facebook's way of telling you not to spam.

Upvotes: 1

Related Questions