Skip to main content

Simple Client Credentials Example

A Quick cmdline test via curl

  • To Request Access Token via basic auth

#!/bin/bash
CLIENT_ID=${CLIENT_ID:cc-client1a}
CLIENT_SECRET=${CLIENT_SECRET}
TOKEN_URL="https://login-uat.stanford.edu/idp/profile/oidc/token"

echo Request Token from ${TOKEN_URL}
response=$(curl -s -X POST $TOKEN_URL \
    -d "grant_type=client_credentials" \
    -d "scope=read" \
    -u "$CLIENT_ID:$CLIENT_SECRET")

access_token=$(echo $response | jq -r '.access_token')
echo $access_token
  • Make API requests with the access token

APIBASE_URL="https://fooapi-dev.stanford.edu"
API_URL=${APIBASE_URL}/book/1

curl -H "Authorization: Bearer ${access_token}" "${API_URL}"
  • To Request Access Token via POST
  #!/bin/bash
  CLIENT_ID=${CLIENT_ID:cc-client1a}
  CLIENT_SECRET=${CLIENT_SECRET}
  TOKEN_URL="https://login-uat.stanford.edu/idp/profile/oidc/token"

  # Output the token request status
  echo "Request Token from ${TOKEN_URL}"

  # Send a POST request to the token endpoint using form data for client credentials
  response=$(curl -s -X POST $TOKEN_URL \
      -d "grant_type=client_credentials" \
      -d "client_id=${CLIENT_ID}" \
      -d "client_secret=${CLIENT_SECRET}" \
      -d "scope=read")

  # Extract the access token from the response
  access_token=$(echo $response | jq -r '.access_token')
  • Make API requests with the access token
  APIBASE_URL="https://fooapi-dev.stanford.edu"
  API_URL="${APIBASE_URL}/book/1"
  curl -H "Authorization: Bearer ${access_token}" "${API_URL}"

Sample FastAPI example

  • Books API Server

    • This is a simple booksapi FastAPI service for adding, retrieving and deleting books.
    • Implemented three scopes: read, writea and delete
  • Simple cmdline tools

    • A simple bash command line script to test out via basic auth with desired read and write scope against booksapi
    • Similarly, another simple bash command line script to test out via POST authn with desired read and write scope against booksapi
Last modified