1+ #!/usr/bin/env python3
2+ """
3+ Sample script demonstrating outbound application management with Descope Python SDK.
4+
5+ This script shows how to:
6+ 1. Create outbound applications (OAuth/OIDC providers)
7+ 2. Manage application settings
8+ 3. Handle user tokens for outbound applications
9+
10+ Outbound applications allow Descope to connect to external OAuth/OIDC providers
11+ like Google, Microsoft, Facebook, etc. as a client.
12+ """
13+
14+ import os
15+ from descope import DescopeClient
16+
17+ def main ():
18+ # Initialize Descope client
19+ # You'll need to set DESCOPE_PROJECT_ID and DESCOPE_MANAGEMENT_KEY environment variables
20+ project_id = os .getenv ("DESCOPE_PROJECT_ID" )
21+ management_key = os .getenv ("DESCOPE_MANAGEMENT_KEY" )
22+
23+ if not project_id or not management_key :
24+ print ("Please set DESCOPE_PROJECT_ID and DESCOPE_MANAGEMENT_KEY environment variables" )
25+ return
26+
27+ client = DescopeClient (project_id = project_id , management_key = management_key )
28+
29+ try :
30+ # 1. Create a new outbound application (Google OAuth)
31+ print ("Creating Google OAuth outbound application..." )
32+ create_resp = client .mgmt .outbound_application .create_application (
33+ name = "My Google Integration" ,
34+ client_id = "your-google-client-id" ,
35+ client_secret = "your-google-client-secret" ,
36+ template_id = "google" , # Use pre-configured Google template
37+ description = "Integration with Google for user authentication" ,
38+ default_scopes = ["openid" , "profile" , "email" ],
39+ pkce = True ,
40+ access_type = "offline" , # Request refresh tokens
41+ )
42+ app_id = create_resp ["app" ]["id" ]
43+ print (f"✓ Created application with ID: { app_id } " )
44+
45+ # 2. Load the created application
46+ print (f"\n Loading application { app_id } ..." )
47+ app_resp = client .mgmt .outbound_application .load_application (app_id )
48+ print (f"✓ Loaded application: { app_resp ['app' ]['name' ]} " )
49+
50+ # 3. Update the application
51+ print (f"\n Updating application { app_id } ..." )
52+ client .mgmt .outbound_application .update_application (
53+ id = app_id ,
54+ name = "My Updated Google Integration" ,
55+ client_id = "your-google-client-id" ,
56+ description = "Updated description for Google integration" ,
57+ default_scopes = ["openid" , "profile" , "email" , "https://www.googleapis.com/auth/calendar.readonly" ],
58+ )
59+ print ("✓ Application updated successfully" )
60+
61+ # 4. List all outbound applications
62+ print ("\n Listing all outbound applications..." )
63+ all_apps_resp = client .mgmt .outbound_application .load_all_applications ()
64+ apps = all_apps_resp ["apps" ]
65+ print (f"✓ Found { len (apps )} outbound applications:" )
66+ for app in apps :
67+ print (f" - { app ['name' ]} (ID: { app ['id' ]} )" )
68+
69+ # 5. Token management examples (these would typically be called after users authenticate)
70+ print (f"\n Token management examples for application { app_id } ..." )
71+
72+ # Note: These operations would typically be performed after a user has authenticated
73+ # and you have actual user IDs and tokens to work with
74+
75+ # Example: Fetch user token for outbound application
76+ # This would retrieve stored OAuth tokens for a user from the external provider
77+ print ("Example: Fetching user token..." )
78+ try :
79+ token_resp = client .mgmt .outbound_application .fetch_outbound_app_user_token (
80+ user_id = "example-user-id" ,
81+ app_id = app_id ,
82+ scopes = ["openid" , "profile" ],
83+ )
84+ print ("✓ Token fetch would work for authenticated users" )
85+ except Exception as e :
86+ print (f"ℹ Token fetch example (would work with real user): { str (e )[:100 ]} ..." )
87+
88+ # Example: Delete user tokens for an application
89+ print ("Example: Deleting user tokens..." )
90+ try :
91+ client .mgmt .outbound_application .delete_outbound_app_user_tokens (
92+ user_id = "example-user-id" ,
93+ app_id = app_id ,
94+ )
95+ print ("✓ Token deletion would work for authenticated users" )
96+ except Exception as e :
97+ print (f"ℹ Token deletion example (would work with real user): { str (e )[:100 ]} ..." )
98+
99+ # 6. Clean up - delete the application
100+ print (f"\n Cleaning up - deleting application { app_id } ..." )
101+ client .mgmt .outbound_application .delete_application (app_id )
102+ print ("✓ Application deleted successfully" )
103+
104+ print ("\n 🎉 Outbound application management demo completed successfully!" )
105+
106+ except Exception as e :
107+ print (f"❌ Error: { e } " )
108+
109+ if __name__ == "__main__" :
110+ main ()
0 commit comments