v1.31.0
conocimientopostman-collectionpatterns/

azure-ad-auth.md

Referencia que postman-collection carga bajo demanda desde su SKILL.md.

postman-collectionskill
patternscarpeta
4376 Btamaño
2026-09-08último cambio del pack

Purpose: Configure Postman collections for APIs protected by Azure AD. Version: 1.0.0


Environment Variables per Environment

Development

VariableValueType
tenantId{dev-tenant-id}default
clientId{dev-client-id}default
clientSecret{dev-client-secret}secret
scopeapi://{dev-client-id}/.defaultdefault
azureAdTokenUrlhttps://login.microsoftonline.com/{dev-tenant-id}/oauth2/v2.0/tokendefault

Pre-production / Production

Same structure, different values per App Registration. Each environment has its own App Registration in Azure portal.

Important: Never share client secrets between environments. Each environment should use its own App Registration with appropriate permissions.


Pre-Request Script (Collection Level)

Use auth-prerequest-azuread.js.template at the collection level. It handles:

  1. Token caching - Stores token in environment variable
  2. Expiry check - 60-second buffer before requesting new token
  3. Error handling - Console error with descriptive messages
  4. Automatic refresh - Requests new token when expired

Client Credentials Flow (Machine-to-Machine)

// This is the most common flow for API testing at the organization
// App Registration → API Permissions → Application permissions
grant_type: "client_credentials"
scope: "api://{clientId}/.default"

On-Behalf-Of Flow (User Delegation)

For APIs that require user context (e.g., accessing downstream APIs on behalf of the user):

// Requires user token first (from login or Azure AD auth code flow)
pm.sendRequest({
    url: pm.environment.get("azureAdTokenUrl"),
    method: "POST",
    header: { "Content-Type": "application/x-www-form-urlencoded" },
    body: {
        mode: "urlencoded",
        urlencoded: [
            { key: "grant_type", value: "urn:ietf:params:oauth:grant-type:jwt-bearer" },
            { key: "client_id", value: pm.environment.get("clientId") },
            { key: "client_secret", value: pm.environment.get("clientSecret") },
            { key: "assertion", value: pm.environment.get("userToken") },
            { key: "scope", value: pm.environment.get("downstreamScope") },
            { key: "requested_token_use", value: "on_behalf_of" }
        ]
    }
}, function (err, res) {
    if (!err && res.code === 200) {
        pm.environment.set("downstreamToken", res.json().access_token);
    }
});

Collection Auth Object

{
    "auth": {
        "type": "bearer",
        "bearer": [
            {
                "key": "token",
                "value": "{{token}}",
                "type": "string"
            }
        ]
    }
}

App Registration Differences per Environment

SettingDevPrePro
TenantSame or separateStaging tenantProduction tenant
Redirect URIshttps://localhost:*https://pre.example.orghttps://api.example.org
API PermissionsAll scopesRestrictedMinimal required
Token LifetimeDefault (1h)Default (1h)May be shorter
Secret Expiry2 years1 year6 months

Newman CI/CD Integration

# Pass Azure AD credentials via CLI environment variables
newman run collection.json \
    -e environment_dev.json \
    --env-var "clientSecret=$AZURE_CLIENT_SECRET" \
    --env-var "tenantId=$AZURE_TENANT_ID" \
    --env-var "clientId=$AZURE_CLIENT_ID"

For Azure DevOps pipelines:

- script: |
    newman run collection.json \
      -e environment_dev.json \
      --env-var "clientSecret=$(AzureClientSecret)" \
      --env-var "tenantId=$(AzureTenantId)"
  displayName: "Run Postman Collection with Azure AD"

Troubleshooting

ErrorCauseFix
AADSTS700016Wrong clientIdVerify App Registration
AADSTS7000218Missing client_secretCheck secret not expired
AADSTS65001Missing API permissionAdd permission + admin consent
AADSTS50011Wrong redirect URINot applicable for client_credentials
AADSTS700024Wrong grant typeUse client_credentials for M2M

Pattern v1.0.0 - Azure AD Authentication for Postman

← Volver a la ficha de postman-collection

Ovillo v1.31.0tag v1.31.0 (2026-10-06) · sin decisiones posteriores69 skills · 19 agents · 22 hooks · 3 reglas · 15 tools · 103 decisionesfuente: ovillo/.claude/skills/postman-collection/patterns/azure-ad-auth.mdCopyright 2026 Hugo Valer Rojas · Apache-2.0