PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/ChineBlog/social/google/__init__.py

https://bitbucket.org/young0512/chineblog
Python | 90 lines | 70 code | 18 blank | 2 comment | 11 complexity | 3569a3ea16f850e4268445dd5b1fdc87 MD5 | raw file
 1#!/usr/bin/env python
 2#coding=utf-8
 3
 4import urllib2, json, urllib, time
 5
 6from django.conf import settings
 7
 8def get_access_token(code):
 9    auth_url = settings.GOOGLE_ACCESS_TOKEN_ENDPOINT
10    body = urllib.urlencode({
11                'code': code,
12                'client_id': settings.GOOGLE_API['client_id'],
13                'client_secret': settings.GOOGLE_API['client_secret'],
14                'redirect_uri': settings.GOOGLE_API['redirect_urls'],
15                'grant_type': 'authorization_code'
16                })
17    headers = {
18        'Content-Type': 'application/x-www-form-urlencoded',
19    }
20    req = urllib2.Request(auth_url, body, headers)
21    resp = urllib2.urlopen(req)
22    
23    data = json.loads(resp.read())
24    
25    return data['access_token']
26
27def get_user_info(access_token):
28    if access_token:
29        userinfo_url = settings.GOOGLE_USERINFO_ENDPOINT
30        query_string = urllib.urlencode({'access_token': access_token})
31        
32        resp = urllib2.urlopen("%s?%s" % (userinfo_url, query_string))
33        data = json.loads(resp.read())
34        
35        return data
36    
37def get_blog_user(user_data):
38    if user_data:
39        blog_user = {}
40        
41        blog_user['username'] = user_data['name']
42        blog_user['email'] = user_data['email']
43        blog_user['avatar'] = user_data['picture']
44        
45        return blog_user
46    
47def get_short_url(url):
48    data = json.dumps({
49        'longUrl': url, 
50    })
51    headers = {"Content-Type": "application/json"}
52    req = urllib2.Request(settings.GOOGLE_URL_SHORTENER_ENDPOINT, data, headers)
53    resp = urllib2.urlopen(req)
54    
55    return json.loads(resp.read()).get('id', None)
56    
57class GooglePlusClient(object):
58    def __init__(self, **kwargs):
59        if 'access_token' in kwargs:
60            self.access_token = kwargs['access_token']
61        if 'refresh_token' in kwargs:
62            self.refresh_token = kwargs['refresh_token']
63            
64        if 'expires' in kwargs:
65            self.expires = kwargs['expires']
66        elif 'expires_in' in kwargs:
67            self.expires = time.time() + kwargs['expires_in']
68            
69    def _refresh_token(self):
70        auth_url = settings.GOOGLE_ACCESS_TOKEN_ENDPOINT
71        body = urllib.urlencode({
72                    'client_id': settings.GOOGLE_API['client_id'],
73                    'client_secret': settings.GOOGLE_API['client_secret'],
74                    'refresh_token': self.refresh_token,
75                    'grant_type': 'refresh_token'
76                    })
77        headers = {
78            'Content-Type': 'application/x-www-form-urlencoded',
79        }
80        req = urllib2.Request(auth_url, body, headers)
81        resp = urllib2.urlopen(req)
82        
83        data = json.loads(resp.read())
84        self.access_token = data['access_token']
85        self.expires = time.time() + data['expires_in']
86    
87    def _get_access_token(self):
88        if not hasattr(self, 'access_token'):
89            self._refresh_token()
90        return self.access_token