This recipe shows the core OAuth 2.0 flow for Marketplace Apps: redirect users to Samsara, exchange the authorization code for credentials, use the access token, refresh expired credentials, and revoke credentials.Documentation Index
Fetch the complete documentation index at: https://samsara-showcase.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
How it works
Import dependencies
Use your language’s web framework, HTTP client, session storage, cryptography, and Base64 utilities.
Load app credentials
Creating a new OAuth 2.0 app gives you an App ID and App Secret. Store them securely, such as in environment variables.
Request app authorization
Redirect users to
https://api.samsara.com/oauth2/authorize with client_id, state, response_type=code, and redirect_uri.Define the OAuth callback
Samsara redirects users back to your app’s configured redirect URI after they authorize the app.
Verify state
Compare the returned
state with the value stored in the user’s session to help prevent CSRF attacks.Exchange the authorization code
Send a
POST request to https://api.samsara.com/oauth2/token with Basic authentication and grant_type=authorization_code.Save credentials
Store
access_token, refresh_token, and an expiration timestamp. Use durable storage in production.Use the access token
Include the access token in API requests using the
Authorization: Bearer TOKEN header.Refresh expired credentials
If the access token expires, send a
POST request to /oauth2/token with grant_type=refresh_token.