Пример #1
0
 def data_with_google(self, access_token):
     try:
         ret = urlopen("https://www.googleapis.com/plus/v1/people/me?" +
                               urlencode({'access_token': access_token})).read()
         email_ret = urlopen("https://www.googleapis.com/oauth2/v3/userinfo?" +
                                     urlencode({'access_token': access_token, 'fields': 'email'})).read()
         email = json.loads(email_ret)['email']
         user_data = json.loads(ret)
         if not user_data['isPlusUser'] or not email:
             return None, None
         google_id = user_data['id']
         name = user_data['displayName']
         try:
             locale = user_data['language']
         except KeyError:
             locale = "en"
         gender = user_data.get('gender', 'unknown')
         if gender == 'male':
             gender = 'M'
         elif gender == 'female':
             gender = 'F'
         # location = user_data.get('currentLocation','')
         photo_url = user_data.get('image', {'url': None}).get('url')
         if photo_url:
             img_temp = NamedTemporaryFile()
             img_temp.write(urlopen(photo_url).read())
             img_temp.flush()
             img_temp.seek(0)
             photo = File(img_temp)
         else:
             photo = None
         data = {'name': name, 'locale': locale, 'email': email, 'gender': gender, 'google_id': google_id}
         if photo:
             files = {'photo': photo}
         else:
             files = None
         return data, files
     except HTTPError:
         return None, None
Пример #2
0
    def user_from_google_access_token(access_token):
        try:
            ret = urlopen("https://www.googleapis.com/plus/v1/people/me?" +
                                  urlencode({'access_token': access_token})).read()
        except HTTPError:
            return None

        user_data = json.loads(ret)
        try:
            user_google = UserGoogle.objects.get(google_id=user_data['id'])
            user_google.save()
            return user_google.user
        except UserGoogle.DoesNotExist:
            return None