facebook.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import requests
  2. import urllib
  3. import simplejson as json
  4. ##########################
  5. # FACEBOOK API CONSTANTS #
  6. ##########################
  7. AUTHORIZE_URL = 'https://graph.facebook.com/oauth/authorize'
  8. ACCESS_TOKEN_URL = 'https://graph.facebook.com/oauth/access_token'
  9. API_URL = 'https://graph.facebook.com/v2.3/'
  10. REQUEST_PERMISSIONS_URL = "https://www.facebook.com/dialog/oauth?"
  11. class FacebookOauthClient(object):
  12. '''
  13. Python client for Facebook API
  14. '''
  15. access_token = None
  16. permission_request_url = None
  17. def __init__(self, client_id, client_secret):
  18. '''
  19. Parameters:
  20. client_id: String
  21. - The client id from the registering app on Facebook
  22. client_secret: String
  23. - The client secret from the registering app on Facebook
  24. '''
  25. self.client_id = client_id
  26. self.client_secret = client_secret
  27. def get_authorize_url(self):
  28. '''
  29. Obtains authorize url link with given client_id.
  30. Returns:
  31. authURL: String
  32. - The authorization url.
  33. '''
  34. authSettings = {'redirect_uri': "http://localhost:8000/hackathon/",
  35. 'client_id': self.client_id}
  36. params = urllib.urlencode(authSettings)
  37. return AUTHORIZE_URL + '?' + params
  38. def get_access_token(self, code):
  39. '''
  40. Obtains access token.
  41. Parameters:
  42. code: String
  43. - The code is retrieved from the authorization url parameter
  44. to obtain access_token.
  45. '''
  46. authSettings = {'code': code,
  47. 'redirect_uri': "http://localhost:8000/hackathon/",
  48. 'client_secret': self.client_secret,
  49. 'client_id': self.client_id}
  50. params = urllib.urlencode(authSettings)
  51. response = requests.get(ACCESS_TOKEN_URL + '?' + params)
  52. if response.status_code != 200:
  53. raise(Exception('Invalid response,response code: {c}'.format(c=response.status_code)))
  54. response_array = str(response.text).split('&')
  55. self.access_token = str(response_array[0][13:])
  56. def get_user_info(self):
  57. '''
  58. Obtains user information.
  59. Returns:
  60. content: Dictionary
  61. - A dictionary containing user information.
  62. '''
  63. response = requests.get("https://graph.facebook.com/me?access_token={at}".format(at=self.access_token))
  64. if response.status_code != 200:
  65. raise(Exception('Invalid response,response code: {c}'.format(c=response.status_code)))
  66. return response.json()
  67. def get_user_likes(self):
  68. '''
  69. Obtains a list of all the user likes. Require a special permission
  70. via Facebook.
  71. Returns:
  72. content: dictionary
  73. -A dictionary containing user likes.
  74. '''
  75. #Check if permission exists or ask for it
  76. if not self.check_permissions('user_likes'):
  77. requestedPermissionUrl = self.request_permissions('user_likes')
  78. #Get likes
  79. response = requests.get(API_URL + 'me/likes?access_token={at}'.format(at=self.access_token))
  80. return response.json()['data']
  81. def check_permissions(self, perm):
  82. '''
  83. Checks if the app has the specified permission.
  84. Parameters:
  85. perm: String
  86. - The desired permission (such as user_likes)
  87. Returns:
  88. bool
  89. - True if the permission granted or false otherwise.
  90. '''
  91. permDict = {'status': 'granted', 'permission':perm}
  92. response = requests.get(API_URL + 'me/permissions?access_token={at}'.format(at=self.access_token))
  93. if response.status_code != 200:
  94. raise(Exception('Invalid response,response code: {c}'.format(c=response.status_code)))
  95. currentPermissions = response.json()['data']
  96. if permDict in currentPermissions:
  97. return True
  98. return False
  99. def request_permissions(self, perm):
  100. '''
  101. Requests a permission from the user.
  102. Parameters:
  103. perm: String
  104. - The permission we would like to get.
  105. Returns: String
  106. - The URL to redirect the user in order to get the permission.
  107. '''
  108. authSettings = {'client_id' : self.client_id,
  109. 'redirect_uri' : 'http://localhost:8000/hackathon/',
  110. 'auth_type' : 'rerequest',
  111. 'scope' : perm,
  112. 'access_token' : access_token}
  113. params = urllib.urlencode(authSettings)
  114. self.permission_request_url = REQUEST_PERMISSIONS_URL + '?' + params