Spaces:
Sleeping
Sleeping
Update app.py
#3
by
Yaswanth56 - opened
app.py
CHANGED
|
@@ -1,26 +1,46 @@
|
|
| 1 |
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
|
| 2 |
from flask_session import Session # Import the Session class
|
| 3 |
from flask.sessions import SecureCookieSessionInterface # Import the class
|
| 4 |
-
from
|
| 5 |
from flask_cors import CORS
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
from flask import Flask, redirect, request, jsonify, session
|
| 9 |
import requests
|
| 10 |
-
import os
|
| 11 |
|
|
|
|
| 12 |
app = Flask(__name__)
|
| 13 |
app.secret_key = os.urandom(24) # Random secret key for session management
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
SALESFORCE_OAUTH_URL = "https://login.salesforce.com/services/oauth2/authorize"
|
| 17 |
SALESFORCE_TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
|
| 18 |
-
CLIENT_ID = '
|
| 19 |
-
CLIENT_SECRET = '
|
| 20 |
-
REDIRECT_URI = 'https://huggingface.co/spaces/nagasurendra/BiryaniHubflask21' # Your Hugging Face redirect URI
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
@app.route('/oauth/callback')
|
| 25 |
def oauth_callback():
|
| 26 |
auth_code = request.args.get('code') # The code returned from Salesforce
|
|
@@ -41,8 +61,8 @@ def oauth_callback():
|
|
| 41 |
|
| 42 |
if token_response.status_code == 200:
|
| 43 |
token_info = token_response.json()
|
| 44 |
-
access_token = token_info['sSSjyhInIsUohKpG8sHzty2q'] #
|
| 45 |
-
instance_url = token_info['https://
|
| 46 |
|
| 47 |
# Store access token in session for future API requests
|
| 48 |
session['access_token'] = access_token
|
|
@@ -52,15 +72,6 @@ def oauth_callback():
|
|
| 52 |
else:
|
| 53 |
return jsonify({"error": "Failed to authenticate with Salesforce"}), 400
|
| 54 |
|
| 55 |
-
|
| 56 |
-
# OAuth flow to redirect to Salesforce login
|
| 57 |
-
//@app.route('/login')
|
| 58 |
-
def login():
|
| 59 |
-
oauth_url = f"{SALESFORCE_OAUTH_URL}?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}"
|
| 60 |
-
return redirect(oauth_url)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
# Example protected route that uses the Salesforce access token
|
| 65 |
@app.route('/dashboard')
|
| 66 |
def dashboard():
|
|
@@ -81,7 +92,5 @@ def dashboard():
|
|
| 81 |
else:
|
| 82 |
return jsonify({"error": "Failed to fetch user info from Salesforce"}), 400
|
| 83 |
|
| 84 |
-
|
| 85 |
if __name__ == '__main__':
|
| 86 |
-
app.run(debug=True)
|
| 87 |
-
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
|
| 2 |
from flask_session import Session # Import the Session class
|
| 3 |
from flask.sessions import SecureCookieSessionInterface # Import the class
|
| 4 |
+
from simple_salesforce import Salesforce
|
| 5 |
from flask_cors import CORS
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
import requests
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize Flask app
|
| 10 |
app = Flask(__name__)
|
| 11 |
app.secret_key = os.urandom(24) # Random secret key for session management
|
| 12 |
|
| 13 |
+
# Configure session management
|
| 14 |
+
app.config["SESSION_COOKIE_SECURE"] = False # Temporarily disable secure cookie
|
| 15 |
+
app.config["SESSION_COOKIE_SAMESITE"] = "Lax" # Use "Lax" instead of "None"
|
| 16 |
+
app.config["SESSION_TYPE"] = "filesystem" # Use filesystem for session storage
|
| 17 |
+
Session(app) # Initialize the session interface
|
| 18 |
+
CORS(app) # Enable Cross-Origin Resource Sharing (CORS)
|
| 19 |
+
|
| 20 |
+
# Salesforce OAuth URLs and credentials from environment variables for security
|
| 21 |
SALESFORCE_OAUTH_URL = "https://login.salesforce.com/services/oauth2/authorize"
|
| 22 |
SALESFORCE_TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
|
| 23 |
+
CLIENT_ID = os.getenv('SALESFORCE_CLIENT_ID', 'your_client_id') # Use environment variables
|
| 24 |
+
CLIENT_SECRET = os.getenv('SALESFORCE_CLIENT_SECRET', 'your_client_secret') # Use environment variables
|
| 25 |
+
REDIRECT_URI = os.getenv('SALESFORCE_REDIRECT_URI', 'https://huggingface.co/spaces/nagasurendra/BiryaniHubflask21') # Your Hugging Face redirect URI
|
| 26 |
+
|
| 27 |
+
# Salesforce connection setup
|
| 28 |
+
def get_salesforce_connection():
|
| 29 |
+
# Fetch Salesforce credentials from environment variables
|
| 30 |
+
sf = Salesforce(
|
| 31 |
+
username=os.getenv('SALESFORCE_USERNAME', 'your_username'),
|
| 32 |
+
password=os.getenv('SALESFORCE_PASSWORD', 'your_password'),
|
| 33 |
+
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN', 'your_security_token')
|
| 34 |
+
)
|
| 35 |
+
return sf
|
| 36 |
|
| 37 |
+
# OAuth flow to redirect to Salesforce login
|
| 38 |
+
@app.route('/login')
|
| 39 |
+
def login():
|
| 40 |
+
oauth_url = f"{SALESFORCE_OAUTH_URL}?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}"
|
| 41 |
+
return redirect(oauth_url)
|
| 42 |
|
| 43 |
+
# Handle the OAuth callback from Salesforce and exchange the authorization code for an access token
|
| 44 |
@app.route('/oauth/callback')
|
| 45 |
def oauth_callback():
|
| 46 |
auth_code = request.args.get('code') # The code returned from Salesforce
|
|
|
|
| 61 |
|
| 62 |
if token_response.status_code == 200:
|
| 63 |
token_info = token_response.json()
|
| 64 |
+
access_token = token_info['sSSjyhInIsUohKpG8sHzty2q'] # Correct key to fetch the access token
|
| 65 |
+
instance_url = token_info['https://huggingface.co/spaces/Yaswanth56/python_test'] # Correct key to fetch the instance URL
|
| 66 |
|
| 67 |
# Store access token in session for future API requests
|
| 68 |
session['access_token'] = access_token
|
|
|
|
| 72 |
else:
|
| 73 |
return jsonify({"error": "Failed to authenticate with Salesforce"}), 400
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Example protected route that uses the Salesforce access token
|
| 76 |
@app.route('/dashboard')
|
| 77 |
def dashboard():
|
|
|
|
| 92 |
else:
|
| 93 |
return jsonify({"error": "Failed to fetch user info from Salesforce"}), 400
|
| 94 |
|
|
|
|
| 95 |
if __name__ == '__main__':
|
| 96 |
+
app.run(debug=True, host="0.0.0.0", port=7860) # Make sure it listens on the correct port
|
|
|