Skip to content

Commit c6f80ac

Browse files
authored
Merge pull request #286516 from vicancy/patch-19
update the doc ito include a curl version
2 parents 24ff946 + c8623ba commit c6f80ac

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

articles/azure-web-pubsub/howto-generate-client-access-url.md

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ The same Client Access URL can be generated by using the Web PubSub server SDK.
4040

4141
2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`:
4242

43-
- Generate MQTT client access token
43+
- Generate client access token
4444

4545
```js
46+
// for web pubsub native clients
47+
let token = await serviceClient.getClientAccessToken();
48+
49+
// for mqtt clients
4650
let token = await serviceClient.getClientAccessToken({ clientProtocol: "mqtt" });
4751
```
4852

@@ -90,9 +94,13 @@ The same Client Access URL can be generated by using the Web PubSub server SDK.
9094

9195
2. Generate Client Access URL by calling `WebPubSubServiceClient.GetClientAccessUri`:
9296

93-
- Generate MQTT client access token
97+
- Generate client access token
9498

9599
```csharp
100+
// for web pubsub native clients
101+
var url = service.GetClientAccessUri();
102+
103+
// for mqtt clients
96104
var url = service.GetClientAccessUri(clientProtocol: WebPubSubClientProtocol.Mqtt);
97105
```
98106

@@ -132,9 +140,13 @@ The same Client Access URL can be generated by using the Web PubSub server SDK.
132140

133141
2. Generate Client Access URL by calling `WebPubSubServiceClient.get_client_access_token`:
134142

135-
- Generate MQTT client access token
143+
- Generate client access token
136144

137145
```python
146+
# for web pubsub native clients
147+
token = service.get_client_access_token();
148+
149+
# for mqtt clients
138150
token = service.get_client_access_token(client_protocol="MQTT")
139151
```
140152

@@ -174,7 +186,14 @@ The same Client Access URL can be generated by using the Web PubSub server SDK.
174186

175187
2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`:
176188

177-
- Generate MQTT client access token
189+
- Generate client access token for Web PubSub native clients
190+
191+
```java
192+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
193+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
194+
```
195+
196+
- Generate client access token for MQTT clients
178197

179198
```java
180199
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
@@ -225,3 +244,71 @@ The same Client Access URL can be generated by using the Web PubSub server SDK.
225244
---
226245

227246
In real-world code, we usually have a server side to host the logic generating the Client Access URL. When a client request comes in, the server side can use the general authentication/authorization workflow to validate the client request. Only valid client requests can get the Client Access URL back.
247+
248+
## Generate from REST API `:generateToken`
249+
You could also use Microsoft Entra ID and generate the token by invoking [Generate Client Token REST API](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token).
250+
251+
> [!NOTE]
252+
> Web PubSub does not recommend that you create Microsoft Entra ID tokens for Microsoft Entra ID service principals manually. This is because each Microsoft Entra ID token is short-lived, typically expiring within one hour. After this time, you must manually generate a replacement Microsoft Entra ID token. Instead, use [our SDKs](#generate-from-service-sdk) that automatically generate and replace expired Microsoft Entra ID tokens for you.
253+
254+
1. Follow [Authorize from application ](./howto-authorize-from-application.md#add-a-client-secret) to enable Microsoft Entra ID and add a client secret.
255+
256+
1. Gather the following information:
257+
258+
| Value name | How to get the value |
259+
| --- | --- |
260+
| TenantId | TenantId is the value of **Directory (tenant) ID** on the **Overview** pane of the application you registered. |
261+
| ClientId | ClientId is the value of **Application (client) ID** from the **Overview** pane of the application you registered. |
262+
| ClientSecret | ClientSecret is the value of the client secret you just added in step #1 |
263+
264+
1. Get the Microsoft Entra ID token from Microsoft identity platform
265+
266+
We use [CURL](https://curl.se/) tool to show how to invoke the REST APIs. The tool is bundled into Windows 10/11, and you could install the tool following [Install CURL](https://curl.se/download.html).
267+
268+
```bash
269+
# set neccessory values, replace the placeholders with your actual values
270+
export TenantId=<your_tenant_id>
271+
export ClientId=<your_client_id>
272+
export ClientSecret=<your_client_secret>
273+
274+
curl -X POST "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" \
275+
-H "Content-Type: application/x-www-form-urlencoded" \
276+
--data-urlencode "grant_type=client_credentials" \
277+
--data-urlencode "client_id=$ClientId" \
278+
--data-urlencode "client_secret=$ClientSecret" \
279+
--data-urlencode "scope=https://webpubsub.azure.com/.default"
280+
281+
```
282+
The above curl command sends a POST request to Microsoft identity endpoint to get the [Microsoft Entra ID token](/entra/identity-platform/id-tokens) back.
283+
In the response you see the Microsoft Entra ID token in `access_token` field. Copy and store it for later use.
284+
285+
1. Use the Microsoft Entra ID token to invoke `:generateToken`
286+
287+
```bash
288+
# Replace the values in {} with your actual values.
289+
export Hostname={your_service_hostname}
290+
export Hub={your_hub}
291+
export Microsoft_Entra_Token={Microsoft_entra_id_token_from_previous_step}
292+
curl -X POST "https://$Hostname/api/hubs/$Hub/:generateToken?api-version=2024-01-01" \
293+
-H "Authorization: Bearer $Microsoft_Entra_Token" \
294+
-H "Content-Type: application/json"
295+
```
296+
297+
If you need to generate the token for MQTT clients, append the `clientType=mqtt` parameter to the URL:
298+
299+
```bash
300+
export Hostname={your_service_hostname}
301+
export Hub={your_hub}
302+
export Microsoft_Entra_Token={Microsoft_entra_id_token_from_previous_step}
303+
curl -X POST "https://$Hostname/api/hubs/$Hub/:generateToken?api-version=2024-01-01&clientType=mqtt" \
304+
-H "Authorization: Bearer $Microsoft_Entra_Token" \
305+
-H "Content-Type: application/json"
306+
```
307+
308+
After running the `cURL` command, you should get a response like this:
309+
310+
```json
311+
{
312+
"token": "ABCDEFG.ABC.ABC"
313+
}
314+
```

0 commit comments

Comments
 (0)