|
| 1 | +#!/usr/bin/python2.7 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (C) 2013 Google Inc. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +import json |
| 18 | +from apiclient.discovery import build_from_document, build |
| 19 | +import httplib2 |
| 20 | +import random |
| 21 | +import pprint |
| 22 | + |
| 23 | +from oauth2client.client import OAuth2WebServerFlow |
| 24 | + |
| 25 | +from flask import Flask, render_template, session, request, redirect, url_for, abort |
| 26 | + |
| 27 | +CLIENT_ID = "YOUR_CLIENT_ID" |
| 28 | +CLIENT_SECRET = 'YOUR_CLIENT_SECRET' |
| 29 | + |
| 30 | +app = Flask(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@app.route('/login') |
| 34 | +def login(): |
| 35 | + flow = OAuth2WebServerFlow(client_id=CLIENT_ID, |
| 36 | + client_secret=CLIENT_SECRET, |
| 37 | + scope='https://www.googleapis.com/auth/glass.timeline', |
| 38 | + redirect_uri='http://localhost:5000/oauth2callback', |
| 39 | + approval_prompt='force', |
| 40 | + access_type='offline') |
| 41 | + |
| 42 | + auth_uri = flow.step1_get_authorize_url() |
| 43 | + return redirect(auth_uri) |
| 44 | + |
| 45 | +@app.route('/signout') |
| 46 | +def signout(): |
| 47 | + del session['credentials'] |
| 48 | + session['message'] = "You have logged out" |
| 49 | + |
| 50 | + return redirect(url_for('index')) |
| 51 | + |
| 52 | +@app.route('/oauth2callback') |
| 53 | +def oauth2callback(): |
| 54 | + code = request.args.get('code') |
| 55 | + if code: |
| 56 | + # exchange the authorization code for user credentials |
| 57 | + flow = OAuth2WebServerFlow(CLIENT_ID, |
| 58 | + CLIENT_SECRET, |
| 59 | + "https://www.googleapis.com/auth/glass.timeline") |
| 60 | + flow.redirect_uri = request.base_url |
| 61 | + try: |
| 62 | + credentials = flow.step2_exchange(code) |
| 63 | + except Exception as e: |
| 64 | + print "Unable to get an access token because ", e.message |
| 65 | + |
| 66 | + # store these credentials for the current user in the session |
| 67 | + session['credentials'] = credentials |
| 68 | + |
| 69 | + return redirect(url_for('index')) |
| 70 | + |
| 71 | +@app.route('/') |
| 72 | +def index(): |
| 73 | + if 'credentials' not in session: |
| 74 | + return redirect(url_for('login')) |
| 75 | + |
| 76 | + credentials = session['credentials'] |
| 77 | + |
| 78 | + http = httplib2.Http() |
| 79 | + http = credentials.authorize(http) |
| 80 | + |
| 81 | + mirror_service = build("mirror", "v1", http=http) |
| 82 | + body = { |
| 83 | + 'notification': {'level': 'DEFAULT'}, |
| 84 | + 'text':'Hello world!' |
| 85 | + } |
| 86 | + |
| 87 | + timeline_item = mirror_service.timeline().insert(body=body).execute() |
| 88 | + |
| 89 | + return "<pre>"+pprint.pformat(timeline_item) |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + app.secret_key = 'hello world' |
| 93 | + app.run(host='0.0.0.0') |
0 commit comments