Advertisement
alfiemax

A sample fb app

Aug 31st, 2014
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf-8
  3.  
  4. # Most of this code I found on the Internet, but I don't recall where.  Don't ask me how it works.
  5. # It just does.  At least it just does as of today, 22 Aug 2013.
  6. #
  7. #  As-is, some weather data from my weather station and an image are posted on
  8. #  Facebook under my user ID.
  9. #  The machine sending the data can post the data without any manual involvement on my part.
  10. #
  11. # I hope this helps somebody.
  12. #
  13. # GDN
  14.  
  15. import facebook
  16. import urllib
  17. import urlparse
  18. import subprocess
  19. import warnings
  20.  
  21. # Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError).
  22. warnings.filterwarnings('ignore', category=DeprecationWarning)
  23.  
  24.  
  25. # Parameters of your app and the id of the profile you want to mess with.
  26. FACEBOOK_APP_ID     = 'your app id here'
  27. FACEBOOK_APP_SECRET = 'your app secret here'
  28. FACEBOOK_PROFILE_ID = 'your profile ID number here'
  29.  
  30.  
  31. # Trying to get an access token. Very awkward. (original author's comment, not mine.  GDN)
  32. oauth_args = dict(client_id     = FACEBOOK_APP_ID,
  33.                   client_secret = FACEBOOK_APP_SECRET,
  34.                   grant_type    = 'client_credentials')
  35. oauth_curl_cmd = ['curl',
  36.                   'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)]
  37. oauth_response = subprocess.Popen(oauth_curl_cmd,
  38.                                   stdout = subprocess.PIPE,
  39.                                   stderr = subprocess.PIPE).communicate()[0]
  40.  
  41. try:
  42.     oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'] [0]  
  43. except KeyError:
  44.     print('Unable to grab an access token!')
  45.     exit()
  46.  
  47. facebook_graph = facebook.GraphAPI(oauth_access_token)
  48.  
  49. # The following line reads a local file containing weather data to post on facebook.  GDN
  50. linestring = open('/var/www/weewx/mobile.html', 'r').read()
  51. print linestring
  52.  
  53. # The following lines attach an image and link to the posting.  GDN
  54. attachment = {
  55.         "name": "nincehelser.com",
  56.         "link": "http://nincehelser.com",
  57.         "caption": "Current Peru Weather",
  58.         "description": linestring,
  59.         "picture": "http://nincehelser.com/kneperu2/image.jpg"
  60.         }
  61.  
  62.  
  63.  
  64.  
  65. # Try to post something on the wall.
  66. try:
  67.     fb_response = facebook_graph.put_wall_post(linestring, attachment, profile_id  = FACEBOOK_PROFILE_ID )
  68.     print fb_response
  69. except facebook.GraphAPIError as e:
  70.     print 'Something went wrong:', e.type, e.message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement