Пример #1
0
    def data_with_facebook(self, access_token):
        # TODO apply facebook v2 api.
        profile = get_object('me', fields="id,name,bio,birthday,locale,timezone,gender,email,picture",
                             access_token=access_token)
        fb_id = profile['id']
        bio = profile.get('bio', '')
        photo_url = "http://graph.facebook.com/%s/picture?width=400&height=400" % fb_id \
            if not profile['picture']['data']['is_silhouette'] else None

        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

        name = profile['name']
        email = profile.get('email', '#' + fb_id + '@inva.lid')
        timezone = profile.get('timezone', 0)
        dob = datetime.datetime.strptime(profile['birthday'], "%m/%d/%Y") if 'birthday' in profile else None
        locale = profile.get('locale', 'en')[:2]
        gender = profile.get('gender', 'U')
        if gender == 'male':
            gender = 'M'
        elif gender == 'female':
            gender = 'F'
        data = {'name': name, 'dob': dob, 'locale': locale, 'email': email,
                'timezone_offset': timezone, 'gender': gender, 'bio': bio, 'fb_id': fb_id}
        if photo:
            files = {'photo': photo}
        else:
            files = None
        return data, files
Пример #2
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
Пример #3
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