Last active
December 18, 2015 01:28
-
-
Save dannyroa/5703953 to your computer and use it in GitHub Desktop.
google oauth2 login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_access_token(request): | |
token = request.GET.get('token', None) | |
if not token: | |
return HttpResponse('error') | |
users = GlassUser.Query.all().eq(token=token) | |
user = None | |
for item in users: | |
user = item | |
if not user: | |
return HttpResponse('error') | |
access_token = get_new_access_token(user.refresh_token) | |
response_dict = {} | |
response_dict['access_token'] = access_token | |
return HttpResponse(simplejson.dumps(response_dict)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import urllib | |
authenticate_url = 'https://accounts.google.com/o/oauth2/auth' | |
access_token_url = 'https://accounts.google.com/o/oauth2/token' | |
def google_login(request): | |
scope = urllib.quote('https://www.googleapis.com/auth/userinfo#email https://www.googleapis.com/auth/glass.timeline') | |
url = '%s?client_id=%s&response_type=code&scope=%s&access_type=offline&redirect_uri=%s%s' % (authenticate_url, settings.GOOGLE_CLIENT_ID, scope, settings.DOMAIN, reverse('callback')) | |
return HttpResponseRedirect(url) | |
def google_callback(request): | |
code = request.GET.get('code','') | |
domain = settings.DOMAIN | |
redirect_uri = urllib2.quote('%s%s' % (domain, reverse('callback'))) | |
params = 'client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=%s' % (settings.GOOGLE_CLIENT_ID, settings.GOOGLE_CLIENT_SECRET, code, redirect_uri) | |
r = request.get(access_token_url, params=params) | |
json_response = r.json() | |
access_token = json_response['access_token'] | |
refresh_token = None | |
if 'refresh_token' in json_response: | |
refresh_token = json_response['refresh_token'] | |
email = get_email(access_token) | |
users = GlassUser.Query.all().eq(email=email) | |
if users.count() == 0: | |
user = GlassUser(email=email) | |
user.token = User.objects.make_random_password(length=16) | |
else: | |
for item in users: | |
user = item | |
user.access_token = access_token | |
if refresh_token: | |
user.refresh_token = refresh_token | |
user.save() | |
create_contact(access_token) | |
subscription_id = create_subscription(access_token, email) | |
user.subscription_id = subscription_id | |
user.save() | |
return HttpResponse('Signed up!') | |
def get_email(access_token): | |
url = 'https://www.googleapis.com/userinfo/email?alt=json' | |
headers = {Authorization' : 'OAuth %s' % access_token} | |
r = requests.post(url, headers=headers) | |
json_response = r.json() | |
email = json_response['data']['email'] | |
return email |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@csrf_exempt | |
def google_receive(request): | |
if not request.body: | |
notification = Notification(email="testing") | |
notification.save() | |
return HttpResponse('error') | |
json_response = simplejson.loads(request.body) | |
timeline_id = json_response['itemId'] | |
email = json_response['userToken'] | |
notification = Notification(email=email) | |
notification.save() | |
users = GlassUser.Query.all().eq(email=email) | |
user = None | |
for item in users: | |
user = item | |
if not user: | |
return HttpResponse('error') | |
access_token = get_new_access_token(user.refresh_token) | |
url = 'https://www.googleapis.com/mirror/v1/timeline/%s/?access_token=%s' % (timeline_id, access_token) | |
r = requests.get(url) | |
json_response = r.json() | |
if 'attachments' in json_response and len(json_response['attachments']) > 0: | |
attachment_id = json_response['attachments'][0]['id'] | |
else: | |
return HttpResponse('error') | |
attachment_exists = TimelineAttachment.Query.all().where(timeline_id=timeline_id, attachment_id=attachment_id, email=email).count() > 0 | |
if not attachment_exists: | |
attachment = TimelineAttachment(timeline_id=timeline_id, attachment_id=attachment_id, email=email) | |
attachment.save() | |
devices = Device.Query.all().eq(email=email) | |
for device in devices: | |
response = send_notification(device.gcm_registration_id, timeline_id, attachment_id) | |
notification.response = response | |
notification.save() | |
return HttpResponse('done') | |
def send_notification(registration_id, timeline_id, attachment_id): | |
url = 'https://android.googleapis.com/gcm/send' | |
payload = {'registration_ids' : [registration_id], 'data' : {'timeline_id' : timeline_id, 'attachment_id' : attachment_id}} | |
headers = {'content-type' : 'application/json', 'Authorization' : 'key=%s' % settings.GOOGLE_API_KEY} | |
r = requests.post(url, headers=headers, data=json.dumps(payload)) | |
return r.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def create_contact(access_token): | |
base_url = 'https://www.googleapis.com/mirror/v1/contacts/' | |
url = '%s%s' % (base_url, settings.GLASS_CONTACT_ID) | |
params = {'access_token' : access_token} | |
r = requests.get(url, params=params) | |
json_response = r.json() | |
already_exists = False | |
if 'error' in json_response: | |
#already exists | |
url = base_url | |
else: | |
already_exists = True | |
payload = { "id": settings.GLASS_CONTACT_ID | |
, "displayName": settings.GLASS_CONTACT_DISPLAY_NAME | |
, "imageUrls": ["https://lh4.googleusercontent.com/-aDvb8790h8I/ULKLr3RzqfI/AAAAAAAAPrc/cdN8aqNiZag/w817-h613-no/IMG_20121125_161759.jpg"] | |
, "acceptTypes" : ['image/jpeg', 'image/png'] | |
} | |
headers = {'content-type': 'application/json' | |
, 'Authorization' : 'Bearer %s' % access_token} | |
if already_exists: | |
r = requests.put(url, data=json.dumps(payload), headers=headers) | |
else: | |
r = requests.post(url, data=json.dumps(payload), headers=headers) | |
def create_subscription(access_token, email): | |
url = 'https://www.googleapis.com/mirror/v1/subscriptions' | |
payload = {'collection' : 'timeline' | |
, 'userToken' : email | |
, 'operation' : ['INSERT', 'UPDATE'] | |
, 'callbackUrl' : settings.GLASS_SUBSCRIPTION_CALLBACK_URL } | |
headers = {'content-type': 'application/json' | |
, 'Authorization' : 'Bearer %s' % access_token} | |
r = requests.post(url, data=json.dumps(payload), headers=headers) | |
return r.json()['id'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment