'Authenticate gspread with already existing session
I'm stuck trying to use gspread on my flask application..
I have this authentication steps
from flask import Flask, url_for, redirect, session, request
from sql_functions import fetch_all, commit, commit_return_last_id, mysql_to_dataframe
from search_routes import search
from author_routes import author
from department_routes import department
from login_routes import authentication
from flask_sslify import SSLify
from settings import SECRET_KEY, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_TOKEN_URI,GOOGLE_AUTH_URI
from authlib.integrations.flask_client import OAuth
import gspread
from oauth2client.client import AccessTokenCredentials
app = Flask(__name__)
app.secret_key = SECRET_KEY
sslify = SSLify(app)
oauth = OAuth(app)
app.register_blueprint(search)
app.register_blueprint(author)
app.register_blueprint(department)
app.register_blueprint(authentication)
# OAuth config
google = oauth.register(
name='google',
client_id=GOOGLE_CLIENT_ID,
client_secret=GOOGLE_CLIENT_SECRET,
access_token_url=GOOGLE_TOKEN_URI,
access_token_params=None,
authorize_url=GOOGLE_AUTH_URI,
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'openid profile email https://www.googleapis.com/auth/spreadsheets'}
)
REFRESH_TOKEN = ""
@app.route('/login_google')
def login_google():
google = oauth.create_client('google')
redirect_url = url_for('authorize', _external=True)
return google.authorize_redirect(redirect_url)
@app.route('/authorize')
def authorize():
google = oauth.create_client('google')
token = google.authorize_access_token()
REFRESH_TOKEN = AccessTokenCredentials(token['access_token'], None)
print (token)
resp = google.get('userinfo')
user_info = resp.json()
session['email'] = user_info['email']
return redirect('/search')
@app.route('/download', methods=['GET','POST'])
def download():
search_id = request.args.get('search_id')
citation_data = mysql_to_dataframe("SELECT some stuff", (search_id,))
gc = gspread.authorize(REFRESH_TOKEN)
sh = gc.create('pubmed_citations')
sh.update([citation_data.columns.values.tolist()] + citation_data.values.tolist())
return redirect('/search')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
I can successfully authenticate using OAuth by going to the /login_google route and then it'll go to /authorize That will take my back to my application, however I want to create a new spreadsheet for my endusers whenever they go to /download but I just can't authenticate gspread using any article I've found over the internet please help me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
