babaTEEpe commited on
Commit
03d0d18
·
verified ·
1 Parent(s): 49f4b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -506,16 +506,23 @@ async def dashboard(request: Request):
506
 
507
  @app.get("/login")
508
  async def login(request: Request):
509
- # Retrieve App redirect URI dynamically from host domain
510
- host = request.headers.get("host", "")
511
- protocol = "https" if "hf.space" in host or request.headers.get("x-forwarded-proto") == "https" else "http"
512
- redirect_uri = f"{protocol}://{host}/callback"
 
 
 
 
 
 
513
 
514
  auth_url = (
515
  f"https://id.ctrader.com/my/settings/openapi/grantingaccess/"
516
  f"?client_id={CLIENT_ID}"
517
  f"&redirect_uri={redirect_uri}"
518
- f"&scope=accounts,trade"
 
519
  )
520
  return RedirectResponse(auth_url)
521
 
@@ -524,9 +531,15 @@ async def callback(request: Request, code: str = None):
524
  if not code:
525
  raise HTTPException(status_code=400, detail="Authorization code missing")
526
 
527
- host = request.headers.get("host", "")
528
- protocol = "https" if "hf.space" in host or request.headers.get("x-forwarded-proto") == "https" else "http"
529
- redirect_uri = f"{protocol}://{host}/callback"
 
 
 
 
 
 
530
 
531
  payload = {
532
  "grant_type": "authorization_code",
 
506
 
507
  @app.get("/login")
508
  async def login(request: Request):
509
+ # Retrieve App redirect URI dynamically (supporting Hugging Face reverse proxy)
510
+ space_host = os.environ.get("SPACE_HOST")
511
+ if space_host:
512
+ redirect_uri = f"https://{space_host}/callback"
513
+ else:
514
+ host = request.headers.get("host", "")
515
+ protocol = "https" if "hf.space" in host or request.headers.get("x-forwarded-proto") == "https" else "http"
516
+ redirect_uri = f"{protocol}://{host}/callback"
517
+
518
+ logger.info(f"Login URL redirect_uri generated: {redirect_uri}")
519
 
520
  auth_url = (
521
  f"https://id.ctrader.com/my/settings/openapi/grantingaccess/"
522
  f"?client_id={CLIENT_ID}"
523
  f"&redirect_uri={redirect_uri}"
524
+ f"&scope=accounts,trading"
525
+ f"&product=web"
526
  )
527
  return RedirectResponse(auth_url)
528
 
 
531
  if not code:
532
  raise HTTPException(status_code=400, detail="Authorization code missing")
533
 
534
+ space_host = os.environ.get("SPACE_HOST")
535
+ if space_host:
536
+ redirect_uri = f"https://{space_host}/callback"
537
+ else:
538
+ host = request.headers.get("host", "")
539
+ protocol = "https" if "hf.space" in host or request.headers.get("x-forwarded-proto") == "https" else "http"
540
+ redirect_uri = f"{protocol}://{host}/callback"
541
+
542
+ logger.info(f"Callback redirect_uri generated: {redirect_uri}")
543
 
544
  payload = {
545
  "grant_type": "authorization_code",