googlePlus.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import requests
  2. import urllib
  3. import simplejson as json
  4. import random
  5. import string
  6. AUTHORISE_URL = 'https://accounts.google.com/o/oauth2/auth'
  7. ACCESS_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
  8. REDIRECT_URL = 'http://localhost:8000/hackathon/'
  9. PROFILE_API = 'https://www.googleapis.com/auth/plus.login'
  10. class GooglePlus:
  11. access_token = None
  12. session_id = None
  13. def __init__(self, client_id, client_secret):
  14. '''
  15. Parameters:
  16. client_id: string
  17. - The client ID from the registering app on Google.
  18. client_secret: string
  19. -The client secret from the registering app on Google.
  20. '''
  21. self.client_id = client_id
  22. self.client_secret = client_secret
  23. def get_session_id(self, length=50):
  24. '''
  25. Generates a random session ID. As a part from the authentication process
  26. we need to verify that the response we get from the server contains the
  27. same session ID as we sent.
  28. Parameters:
  29. length: integer
  30. - The length of the session ID.
  31. '''
  32. chars = string.uppercase + string.digits + string.lowercase
  33. self.session_id = ''.join(random.choice(chars) for _ in range(length))
  34. def get_authorize_url(self):
  35. '''
  36. Obtains authorize url link with the given client_id.
  37. Returns:
  38. authURL: string
  39. - The authorization URL.
  40. '''
  41. self.get_session_id()
  42. authSettings = {'state': self.session_id,
  43. 'redirect_uri':REDIRECT_URL,
  44. 'response_type':'code',
  45. 'client_id':self.client_id,
  46. 'scope': PROFILE_API}
  47. params = urllib.urlencode(authSettings)
  48. return AUTHORISE_URL + '?' + params
  49. def get_access_token(self, code, state):
  50. '''
  51. Obtains access token.
  52. Parameters:
  53. code: string
  54. - The code is retrived from the authorization URL parameter
  55. to obtain access token.
  56. state: string
  57. - The unique session ID.
  58. '''
  59. #Checking that the sessino ID from the response match the session ID we sent
  60. if state != self.session_id:
  61. raise(Exception('Danger! Someone is messing up with you connection!'))
  62. authSettings = {'client_secret': self.client_secret,
  63. 'code':code,
  64. 'grant_type':'authorization_code',
  65. 'client_id': self.client_id,
  66. 'redirect_uri': 'http://localhost:8000/hackathon/'}
  67. response = requests.post(ACCESS_TOKEN_URL, data=authSettings)
  68. if response.status_code != 200:
  69. raise(Exception('Invalid response, response code {c}'.format(c=response.status_code)))
  70. self.access_token = response.json()['access_token']
  71. def get_user_info(self):
  72. '''
  73. Obtain user information.
  74. Returns:
  75. content: dictionary
  76. - A dictionary contains user information.
  77. '''
  78. USER_INFO_API = 'https://www.googleapis.com/oauth2/v2/userinfo'
  79. params = urllib.urlencode({'access_token' : self.access_token})
  80. response = requests.get(USER_INFO_API + '?' + params)
  81. if response.status_code != 200:
  82. raise(Exception('Invalid response, response code {c}'.format(c=response.status_code)))
  83. return response.json()