Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -5,6 +5,82 @@ from salesforce import get_salesforce_connection
5
  from flask_cors import CORS
6
  import os
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Initialize Flask app and Salesforce connection
9
  print("Starting app...")
10
  app = Flask(__name__)
 
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
+ # Salesforce OAuth URLs and credentials
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 = '3MVG9WVXk15qiz1JbtW1tT9a7WojFUbAfMVyVXfvI4PISHAKAxmZ8RLS1lBHqpnaDPQPZOOInuVdcQpi7smWc' # Your Consumer Key
19
+ CLIENT_SECRET = '36C463CD713C420BA2ED78F853359EACCE1ECCE2954C9810FFD7F946564CB0E8' # Your Consumer Secret
20
+ REDIRECT_URI = 'https://huggingface.co/spaces/nagasurendra/BiryaniHubflask21' # Your Hugging Face redirect URI
21
+ INSTANCE_URL = 'https://biryanihub-dev-ed.develop.my.site.com/s/welcomePage' # Your Salesforce instance URL
22
+
23
+ # OAuth flow to redirect to Salesforce login
24
+ @app.route('/login')
25
+ def login():
26
+ oauth_url = f"{SALESFORCE_OAUTH_URL}?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}"
27
+ return redirect(oauth_url)
28
+
29
+ # Handle the OAuth callback from Salesforce and exchange the authorization code for an access token
30
+ @app.route('/oauth/callback')
31
+ def oauth_callback():
32
+ auth_code = request.args.get('code') # The code returned from Salesforce
33
+
34
+ if not auth_code:
35
+ return jsonify({"error": "No authorization code provided"}), 400
36
+
37
+ # Exchange the authorization code for an access token
38
+ token_data = {
39
+ 'grant_type': 'authorization_code',
40
+ 'code': auth_code,
41
+ 'client_id': CLIENT_ID,
42
+ 'client_secret': CLIENT_SECRET,
43
+ 'redirect_uri': REDIRECT_URI
44
+ }
45
+
46
+ token_response = requests.post(SALESFORCE_TOKEN_URL, data=token_data)
47
+
48
+ if token_response.status_code == 200:
49
+ token_info = token_response.json()
50
+ access_token = token_info['sSSjyhInIsUohKpG8sHzty2q']
51
+ instance_url = token_info['https://biryanihub-dev-ed.develop.my.site.com/s/welcomePage'] # The Salesforce instance URL
52
+
53
+ # Store access token in session for future API requests
54
+ session['access_token'] = access_token
55
+ session['instance_url'] = instance_url
56
+
57
+ return redirect('/dashboard') # Redirect user to the Hugging Face dashboard or home page
58
+ else:
59
+ return jsonify({"error": "Failed to authenticate with Salesforce"}), 400
60
+
61
+ # Example protected route that uses the Salesforce access token
62
+ @app.route('/dashboard')
63
+ def dashboard():
64
+ if 'access_token' not in session:
65
+ return redirect('/login') # If the user is not logged in, redirect to login
66
+
67
+ access_token = session['access_token']
68
+ instance_url = session['instance_url']
69
+
70
+ # Use the access token to make API calls to Salesforce (example: fetch user info)
71
+ headers = {'Authorization': f'Bearer {access_token}'}
72
+ user_info_url = f"{instance_url}/services/oauth2/userinfo"
73
+ user_info_response = requests.get(user_info_url, headers=headers)
74
+
75
+ if user_info_response.status_code == 200:
76
+ user_info = user_info_response.json()
77
+ return jsonify(user_info) # Display user info from Salesforce
78
+ else:
79
+ return jsonify({"error": "Failed to fetch user info from Salesforce"}), 400
80
+
81
+ if __name__ == '__main__':
82
+ app.run(debug=True)
83
+
84
  # Initialize Flask app and Salesforce connection
85
  print("Starting app...")
86
  app = Flask(__name__)