-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauth.py
51 lines (37 loc) · 1.38 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os
import schwab
import ssm
import logging
logger = logging.getLogger()
logger.setLevel("INFO")
def auth_handler(event, lambda_context):
logger.info(f"Event: {event}")
logger.info(f"Lambda context: {lambda_context} ")
# Define the authorization endpoint and required parameters
client_id = schwab.get_app_key()
redirect_uri = f"{os.environ['API_URL']}/callback"
scope = 'readonly' # Change this to the actual scopes required by your app
response_type = 'code'
# Construct the authorization URL
authorization_url = f"https://api.schwabapi.com/v1/oauth/authorize?response_type={response_type}&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}"
# Redirect the user to the authorization URL
response = {
'statusCode': 302,
'headers': {
'Location': authorization_url
},
'body': json.dumps('Redirecting to authorization page...')
}
return response
def callback_handler(event, lambda_context):
logger.info(f"Event: {event}")
logger.info(f"Lambda context: {lambda_context} ")
code = event['queryStringParameters']['code']
token_resp = schwab.get_token(code)
ssm.put_secret("/algotrading/schwab/refreshtoken", token_resp["refresh_token"])
# Redirect the user to the authorization URL
response = {
'statusCode': 200
}
return response