|
20 | 20 | "outputs": [], |
21 | 21 | "source": [ |
22 | 22 | "import utils\n", |
23 | | - "import time\n", |
24 | 23 | "from apimtypes import *\n", |
| 24 | + "import os\n", |
25 | 25 | "\n", |
26 | 26 | "# 1) User-defined parameters (change these as needed)\n", |
27 | 27 | "rg_location = 'eastus2'\n", |
|
30 | 30 | "tags = ['oauth-3rd-party', 'jwt', 'credential-manager', 'policy-fragment'] # ENTER DESCRIPTIVE TAG(S)\n", |
31 | 31 | "api_prefix = 'oauth-3rd-party-' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n", |
32 | 32 | "# OAuth\n", |
33 | | - "client_id = 'your-spotify-client-id' # ENTER THE OAUTH CLIENT ID FOR THE BACKEND API\n", |
34 | | - "client_secret = 'your-spotify-client-secret' # ENTER THE OAUTH CLIENT SECRET FOR THE BACKEND API\n", |
| 33 | + "client_id = os.getenv('SPOTIFY_CLIENT_ID') # ENTER THE OAUTH CLIENT ID FOR THE BACKEND API\n", |
| 34 | + "client_secret = os.getenv('SPOTIFY_CLIENT_SECRET') # ENTER THE OAUTH CLIENT SECRET FOR THE BACKEND API\n", |
35 | 35 | "\n", |
36 | 36 | "# 2) Service-defined parameters (please do not change these)\n", |
37 | 37 | "rg_name = utils.get_infra_rg_name(deployment, index)\n", |
38 | 38 | "sample_folder = \"oauth-3rd-party\"\n", |
39 | 39 | "nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.APIM_ACA, INFRASTRUCTURE.SIMPLE_APIM], True)\n", |
40 | 40 | "\n", |
| 41 | + "if len(client_id) == 0 or len(client_secret) == 0:\n", |
| 42 | + " utils.print_error('Please set the SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET environment variables in the root .env file before running this notebook.')\n", |
| 43 | + " raise ValueError('Missing Spotify OAuth credentials')\n", |
| 44 | + "\n", |
41 | 45 | "# 3) Set up the named values\n", |
42 | 46 | "nvs: List[NamedValue] = [\n", |
43 | 47 | " NamedValue(nb_helper.jwt_key_name, nb_helper.jwt_key_value_bytes_b64, True),\n", |
|
107 | 111 | "if output.json_data:\n", |
108 | 112 | " apim_name = output.get('apimServiceName', 'APIM Service Name')\n", |
109 | 113 | " apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n", |
110 | | - "\n", |
111 | | - " # TODO: This should be retrieved from an output; however, the format is static.\n", |
112 | | - " apim_oauth_redirect_url = f'https://authorization-manager.consent.azure-apim.net/redirect/apim/{apim_name}'\n", |
113 | | - " utils.print_val('APIM OAuth Redirect URL', apim_oauth_redirect_url)\n", |
| 114 | + " spotify_oauth_redirect_url = output.get('spotifyOAuthRedirectUrl', 'Spotify OAuth Redirect URL')\n", |
114 | 115 | "\n", |
115 | 116 | "utils.print_ok('Deployment completed')" |
116 | 117 | ] |
|
175 | 176 | "encoded_jwt_token_marketing_member = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.MARKETING_MEMBER), nb_helper.jwt_key_value)\n", |
176 | 177 | "print(f'\\nJWT token for Marketing Member:\\n{encoded_jwt_token_marketing_member}') # this value is used to call the APIs via APIM\n", |
177 | 178 | "\n", |
178 | | - "# Issue requests against Front Door.\n", |
179 | | - "utils.print_message('Checking if the infrastructure architecture deployment uses Azure Front Door.', blank_above = True)\n", |
180 | | - "afd_endpoint_url = utils.get_frontdoor_url(deployment, rg_name)\n", |
181 | | - "\n", |
182 | | - "if afd_endpoint_url:\n", |
183 | | - " artist_id = '06HL4z0CvFAxyc27GXpf02' # Taylor Swift's Spotify Artist ID\n", |
184 | | - " reqsAfd = ApimRequests(afd_endpoint_url)\n", |
185 | | - " reqsAfd.headers['Authorization'] = f'Bearer {encoded_jwt_token_marketing_member}'\n", |
186 | | - " output = reqsAfd.singleGet(f'/oauth-3rd-party-spotify/artists/{artist_id}', msg = 'Calling the Spotify Artist API via API Management Gateway URL.')\n", |
187 | | - " artist = json.loads(output)\n", |
188 | | - " tests.verify(artist['name'], 'Taylor Swift')\n", |
189 | | - " utils.print_info(f'{artist[\"name\"]} has a popularity rating of {artist[\"popularity\"]} with {artist[\"followers\"][\"total\"]:,} followers on Spotify.')\n", |
190 | | - "else:\n", |
191 | | - " # Issue a direct request to API Management\n", |
192 | | - " artist_id = '6XpaIBNiVzIetEPCWDvAFP' # Whitney Houston's Spotify Artist ID\n", |
193 | | - " reqsApim = ApimRequests(apim_gateway_url)\n", |
194 | | - " reqsApim.headers['Authorization'] = f'Bearer {encoded_jwt_token_marketing_member}'\n", |
195 | | - " output = reqsApim.singleGet(f'/oauth-3rd-party-spotify/artists/{artist_id}', msg = 'Calling the Spotify Artist API via API Management Gateway URL.')\n", |
196 | | - " artist = json.loads(output)\n", |
197 | | - " tests.verify(artist['name'], 'Whitney Houston')\n", |
198 | | - " utils.print_info(f'{artist[\"name\"]} has a popularity rating of {artist[\"popularity\"]} with {artist[\"followers\"][\"total\"]:,} followers on Spotify.')\n", |
| 179 | + "# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n", |
| 180 | + "endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n", |
| 181 | + "reqs = ApimRequests(endpoint_url)\n", |
| 182 | + "reqs.headers['Authorization'] = f'Bearer {encoded_jwt_token_marketing_member}'\n", |
| 183 | + "artist_id = '06HL4z0CvFAxyc27GXpf02' # Taylor Swift's Spotify Artist ID\n", |
| 184 | + "output = reqs.singleGet(f'/oauth-3rd-party-spotify/artists/{artist_id}', msg = 'Calling the Spotify Artist API via API Management Gateway URL.')\n", |
| 185 | + "artist = json.loads(output)\n", |
| 186 | + "tests.verify(artist['name'], 'Taylor Swift')\n", |
| 187 | + "utils.print_info(f'{artist[\"name\"]} has a popularity rating of {artist[\"popularity\"]} with {artist[\"followers\"][\"total\"]:,} followers on Spotify.')\n", |
199 | 188 | "\n", |
200 | 189 | "tests.print_summary()\n", |
201 | 190 | "\n", |
|
0 commit comments