from flask import Flask, jsonify, request import gspread from oauth2client.service_account import ServiceAccountCredentials app = Flask(__name__) @app.route('/sample',methods=['POST']) def get_sample_data(): return {"name" : "Ganesh kumar"} @app.route('/data', methods=['POST']) def get_data(): reqData = request.json request_data = reqData['requestdata'] try: scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.from_json_keyfile_name('project-g-sheet-to-json-b23cf6e187cd.json', scope) client = gspread.authorize(credentials) sheet_url = request_data['sheet_url'] spreadsheet = client.open_by_url(sheet_url) sheet_data = {} for sheet in spreadsheet: sheet_name = sheet.title data = sheet.get_all_records() sheet_data[sheet_name] = data respsucc={"responseid": reqData['requestid'], "responsefor": "Sheet Details", "responsesetto": "UI", "response": {"message": "Success", "data" : sheet_data}} print(data) return jsonify(respsucc) except gspread.exceptions.APIError as e: error_message = str(e) respfail = {"responseid": reqData['requestid'], "responsefor": "Sheet Details", "responsesetto": "UI", "response": {"message": error_message}} return jsonify(respfail) # Running app if __name__ == '__main__': app.run(host="0.0.0.0",debug=True)