limited-city-59768
02/13/2024, 7:22 PMchilly-king-10285
02/13/2024, 7:22 PMchilly-king-10285
02/13/2024, 7:23 PMlimited-city-59768
02/13/2024, 7:29 PMchilly-king-10285
02/13/2024, 7:29 PMchilly-king-10285
02/13/2024, 7:29 PM{URL-encoded-client-ID}:{URL-encoded-client-secret}
. Here's an example of how to use Basic Authentication to obtain an access token: javascript const clientID = "the-client-id" const clientSecret = "the-secret-id" const basicAuth = bas64_encode(url_encode(clientID) + ":" + url_encode(clientSecret)) const requestOptions = { method: "POST", headers: { Authorization: "Basic " + basicAuth, "Content-Type": "application/x-www-form-urlencoded", }, body: "grant_type=client_credentials&scope=read", } fetch("<https://your-project.projects.oryapis.com/oauth2/token>", requestOptions) .then((response) => response.json()) .then((data) => console.log(data))
(Source: [Basic Authentication](https://www.ory.sh/docs/oauth2-oidc/client-credentials#basic-authentication)) 2. Body Authentication: The client ID and client secret are sent as parameters in the body of the HTTP request. Here's an example of how to use Body Authentication to obtain an access token: javascript const clientID = "the-client-id" const clientSecret = "the-secret-id" const qs = new URLSearchParams() qs.set("grant_type", "client_credentials") qs.set("client_id", clientID) qs.set("client_secret", clientSecret) qs.set("scope", read) const requestOptions = { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: qs.toString(), } fetch("<https://your-project.projects.oryapis.com/oauth2/token>", requestOptions) .then((response) => response.json()) .then((data) => console.log(data))
(Source: [Body Authentication](https://www.ory.sh/docs/oauth2-oidc/client-credentials#body-authentication)) 3. JWT Bearer Grant Type: Instead of sending the client ID and client secret, the client sends a JSON Web Token (JWT) which was signed by its cryptographic key. The client is created with the following command: bash ory create oauth2-client --project "$project_id" \ --name "Client Credentials Demo" \ --grant-type client_credentials \ --token-endpoint-auth-method private_key_jwt \ --jwks-uri <https://example.org/path/to/clients/public_key_set.jwks>
(Source: [JWT Bearer profile for OAuth2 client authentication Part 1](https://www.ory.sh/docs/oauth2-oidc/client-credentials#jwt-bearer-profile-for-oauth2-client-authentication)) Remember, the client ID and client secret should be kept confidential and not exposed to the public.