euler314 commited on
Commit
92bef0d
·
verified ·
1 Parent(s): 86db263

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -10
app.py CHANGED
@@ -1,4 +1,5 @@
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,21 +8,75 @@ import io
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>
 
1
+ from flask import Flask, request, jsonify, render_template_string, redirect, session
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
  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 configuration
20
+ client_config = {
21
+ "web": {
22
+ "client_id": os.environ.get('GOOGLE_CLIENT_ID'),
23
+ "client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
24
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25
+ "token_uri": "https://oauth2.googleapis.com/token",
26
+ "redirect_uris": ["https://euler314-purchase-record.hf.space/oauth2callback"]
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
  HTML_TEMPLATE = '''
81
  <!DOCTYPE html>
82
  <html>