Skip to content

Commit d1700aa

Browse files
authored
Merge pull request #52873 from anushreeringne/patch-8
Updating Documentation to use MSAL & v2 endpoint
2 parents 2049548 + eb781c1 commit d1700aa

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

articles/hdinsight/kafka/rest-proxy.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ For REST proxy endpoint requests, client applications should get an OAuth token.
6969
You can use the python code below to interact with the REST proxy on your Kafka cluster. To use the code sample, follow these steps:
7070

7171
1. Save the sample code on a machine with Python installed.
72-
1. Install required python dependencies by executing `pip3 install adal` and `pip install msrestazure`.
72+
1. Install required python dependencies by executing `pip3 install msal`.
7373
1. Modify the code section **Configure these properties** and update the following properties for your environment:
7474

7575
|Property |Description |
@@ -79,7 +79,7 @@ You can use the python code below to interact with the REST proxy on your Kafka
7979
|Client Secret|The secret for the application that you registered in the security group.|
8080
|Kafkarest_endpoint|Get this value from the **Properties** tab in the cluster overview as described in the [deployment section](#create-a-kafka-cluster-with-rest-proxy-enabled). It should be in the following format – `https://<clustername>-kafkarest.azurehdinsight.net`|
8181

82-
1. From the command line, execute the python file by executing `python <filename.py>`
82+
1. From the command line, execute the python file by executing `sudo python3 <filename.py>`
8383

8484
This code does the following action:
8585

@@ -90,13 +90,9 @@ For more information on getting OAuth tokens in python, see [Python Authenticati
9090

9191
```python
9292
#Required python packages
93-
#pip3 install adal
94-
#pip install msrestazure
93+
#pip3 install msal
9594

96-
import adal
97-
from msrestazure.azure_active_directory import AdalAuthentication
98-
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD
99-
import requests
95+
import msal
10096

10197
#--------------------------Configure these properties-------------------------------#
10298
# Tenant ID for your Azure Subscription
@@ -109,19 +105,24 @@ client_secret = 'password'
109105
kafkarest_endpoint = "https://<clustername>-kafkarest.azurehdinsight.net"
110106
#--------------------------Configure these properties-------------------------------#
111107

112-
#getting token
113-
login_endpoint = AZURE_PUBLIC_CLOUD.endpoints.active_directory
114-
resource = "https://hib.azurehdinsight.net"
115-
context = adal.AuthenticationContext(login_endpoint + '/' + tenant_id)
108+
# Scope
109+
scope = 'https://hib.azurehdinsight.net/.default'
110+
#Authority
111+
authority = 'https://login.microsoftonline.com/' + tenant_id
116112

117-
token = context.acquire_token_with_client_credentials(
118-
resource,
119-
client_id,
120-
client_secret)
113+
# Create a preferably long-lived app instance which maintains a token cache.
114+
app = msal.ConfidentialClientApplication(
115+
client_id , client_secret, authority,
116+
#cache - For details on how look at this example: https://github.com/Azure-Samples/ms-identity-python-webapp/blob/master/app.py
117+
)
121118

122-
accessToken = 'Bearer ' + token['accessToken']
119+
# The pattern to acquire a token looks like this.
120+
result = None
123121

124-
print(accessToken)
122+
result = app.acquire_token_for_client(scopes=[scope])
123+
124+
print(result)
125+
accessToken = result['access_token']
125126

126127
# relative url
127128
getstatus = "/v1/metadata/topics"
@@ -132,10 +133,10 @@ response = requests.get(request_url, headers={'Authorization': accessToken})
132133
print(response.content)
133134
```
134135

135-
Find below another sample on how to get a token from Azure for REST proxy using a curl command. Notice that we need the `resource=https://hib.azurehdinsight.net` specified while getting a token.
136+
Find below another sample on how to get a token from Azure for REST proxy using a curl command. **Notice that we need the `scope=https://hib.azurehdinsight.net/.default` specified while getting a token.**
136137

137138
```cmd
138-
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=<clientid>&client_secret=<clientsecret>&grant_type=client_credentials&resource=https://hib.azurehdinsight.net' 'https://login.microsoftonline.com/<tenantid>/oauth2/token'
139+
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=<clientid>&client_secret=<clientsecret>&grant_type=client_credentials&scope=https://hib.azurehdinsight.net/.default' 'https://login.microsoftonline.com/<tenantid>/oauth2/v2.0/token'
139140
```
140141

141142
## Next steps

0 commit comments

Comments
 (0)