Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
-
from google_auth_oauthlib.flow import Flow
|
| 3 |
from google.oauth2.credentials import Credentials
|
| 4 |
from googleapiclient.discovery import build
|
| 5 |
from googleapiclient.http import MediaIoBaseUpload
|
|
@@ -8,76 +7,21 @@ import io
|
|
| 8 |
import datetime
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
-
app.secret_key = os.environ.get('FLASK_SECRET_KEY')
|
| 12 |
-
|
| 13 |
-
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
|
| 14 |
-
'https://www.googleapis.com/auth/drive.file']
|
| 15 |
|
| 16 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
| 17 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
| 18 |
|
| 19 |
-
# OAuth
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
}
|
| 29 |
|
| 30 |
-
def credentials_to_dict(credentials):
|
| 31 |
-
return {
|
| 32 |
-
'token': credentials.token,
|
| 33 |
-
'refresh_token': credentials.refresh_token,
|
| 34 |
-
'token_uri': credentials.token_uri,
|
| 35 |
-
'client_id': credentials.client_id,
|
| 36 |
-
'client_secret': credentials.client_secret,
|
| 37 |
-
'scopes': credentials.scopes
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
def get_credentials():
|
| 41 |
-
if 'credentials' not in session:
|
| 42 |
-
return None
|
| 43 |
-
return Credentials(**session['credentials'])
|
| 44 |
-
|
| 45 |
-
@app.route('/authorize')
|
| 46 |
-
def authorize():
|
| 47 |
-
flow = Flow.from_client_config(
|
| 48 |
-
client_config,
|
| 49 |
-
scopes=SCOPES,
|
| 50 |
-
redirect_uri=client_config['web']['redirect_uris'][0]
|
| 51 |
-
)
|
| 52 |
-
authorization_url, state = flow.authorization_url(
|
| 53 |
-
access_type='offline',
|
| 54 |
-
include_granted_scopes='true'
|
| 55 |
-
)
|
| 56 |
-
session['state'] = state
|
| 57 |
-
return redirect(authorization_url)
|
| 58 |
-
|
| 59 |
-
@app.route('/oauth2callback')
|
| 60 |
-
def oauth2callback():
|
| 61 |
-
state = session['state']
|
| 62 |
-
flow = Flow.from_client_config(
|
| 63 |
-
client_config,
|
| 64 |
-
scopes=SCOPES,
|
| 65 |
-
state=state,
|
| 66 |
-
redirect_uri=client_config['web']['redirect_uris'][0]
|
| 67 |
-
)
|
| 68 |
-
authorization_response = request.url
|
| 69 |
-
flow.fetch_token(authorization_response=authorization_response)
|
| 70 |
-
credentials = flow.credentials
|
| 71 |
-
session['credentials'] = credentials_to_dict(credentials)
|
| 72 |
-
return redirect('/')
|
| 73 |
-
|
| 74 |
-
@app.route('/')
|
| 75 |
-
def index():
|
| 76 |
-
if not get_credentials():
|
| 77 |
-
return redirect('/authorize')
|
| 78 |
-
return render_template_string(HTML_TEMPLATE)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
HTML_TEMPLATE = '''
|
| 82 |
<!DOCTYPE html>
|
| 83 |
<html>
|
|
@@ -204,6 +148,7 @@ HTML_TEMPLATE = '''
|
|
| 204 |
</html>
|
| 205 |
'''
|
| 206 |
|
|
|
|
| 207 |
@app.route('/')
|
| 208 |
def index():
|
| 209 |
return render_template_string(HTML_TEMPLATE)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template_string
|
|
|
|
| 2 |
from google.oauth2.credentials import Credentials
|
| 3 |
from googleapiclient.discovery import build
|
| 4 |
from googleapiclient.http import MediaIoBaseUpload
|
|
|
|
| 7 |
import datetime
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
SPREADSHEET_ID = '1rcIJflbC1VIc70F6dnASLFvGQ90TXHhCx0iyxsXR7Ww'
|
| 12 |
FOLDER_ID = '1brmLNqOMCvRS0TDY6ECHvIBmlRx8pcPz'
|
| 13 |
|
| 14 |
+
# OAuth 2.0 credentials
|
| 15 |
+
CREDENTIALS = {
|
| 16 |
+
'token': None,
|
| 17 |
+
'refresh_token': None,
|
| 18 |
+
'token_uri': 'https://oauth2.googleapis.com/token',
|
| 19 |
+
'client_id': os.environ.get('GOOGLE_CLIENT_ID'),
|
| 20 |
+
'client_secret': os.environ.get('GOOGLE_CLIENT_SECRET'),
|
| 21 |
+
'scopes': ['https://www.googleapis.com/auth/spreadsheets',
|
| 22 |
+
'https://www.googleapis.com/auth/drive.file']
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
HTML_TEMPLATE = '''
|
| 26 |
<!DOCTYPE html>
|
| 27 |
<html>
|
|
|
|
| 148 |
</html>
|
| 149 |
'''
|
| 150 |
|
| 151 |
+
# Single route definition for index
|
| 152 |
@app.route('/')
|
| 153 |
def index():
|
| 154 |
return render_template_string(HTML_TEMPLATE)
|