Connecting Your App to KonPro with OAuth 2.0
Securely integrate your application with KonPro using OAuth 2.0
Welcome to OAuth Integration! 🔐
This guide will walk you through connecting your app to KonPro using OAuth 2.0. We'll use curl commands to demonstrate each step, making it easy to test the process. KonPro uses the Authorization Code Flow with PKCE (Proof Key for Code Exchange) for added security.
Prerequisites
Before you begin, ensure you have:
- Your KonPro Client ID
- Your Redirect URI
- (This will be provided by the Partnerships team after you have submitted your Integration Intake form)
Step 1: Initiate User Authorization
To begin the OAuth process, redirect the user to KonPro's authorization URL. Create this URL (replace the capitalized parts with your info):
https://app.konpro.ai/oauth/authorize?client_id=YOUR_CLIENT_ID&state=RANDOM_STATE&redirect_uri=YOUR_REDIRECT_URI&code_challenge=CODE_CHALLENGE&code_challenge_method=S256&response_type=codeYOUR_CLIENT_ID: Client ID. (e.g., abc123)
RANDOM_STATE: A unique string to maintain state. (e.g., xyz789)
YOUR_REDIRECT_URI: Your approved redirect URI, ensure URL-encoded. (e.g., https://example.com/oauth/callback)
CODE_CHALLENGE: Corresponding code_challenge for a generated code_verifier using the PKCE flow (e.g. E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM). See: https://www.rfc-editor.org/rfc/rfc7636#appendix-B.
Step 2: Handle the Authorization Callback
After approval, you'll be redirected to your Redirect URI with a code parameter. It'll look like this:
https://yourapp.com/oauth/callback?code=AUTHORIZATION_CODE&state=RANDOM_STATEVerify that the state matches the one you sent in Step 1, then extract the AUTHORIZATION_CODE.
Step 3: Exchange Authorization Code for Access Token
Exchange the authorization code for an access token using this curl command:
curl -X POST https://api.konpro.ai/v1/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "code=AUTHORIZATION_CODE" \
-d "client_id=YOUR_CLIENT_ID" \
-d "grant_type=authorization_code" \
-d "redirect_uri=YOUR_REDIRECT_URI" \
-d "code_verifier=YOUR_CODE_VERIFIER"Save the access_token and refresh_token - you'll need them!
Step 4: Use the Access Token
Now you can make requests to KonPro's API. Here's an example:
curl -X GET https://api.konpro.ai/v1/some-endpoint \
-H "Authorization: Bearer NEW_ACCESS_TOKEN"The KonPro API endpoints support both authentication methods:
For OAuth Access Tokens:
- Use the Authorization header with the Bearer scheme.
- Format:
Authorization: Bearer YOUR_ACCESS_TOKEN - This is the method enabled by OAuth integration.
For API Tokens:
- Use the X-API-KEY header.
- Format:
X-API-KEY: YOUR_API_TOKEN - This is the existing method.
Step 5: Refresh the Access Token
Access tokens expire. Keep track of token expiration and refresh as needed. When the access token expires, use the refresh token to obtain a new one:
curl -X POST https://api.konpro.ai/v1/oauth/refresh_token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=YOUR_CLIENT_ID" \
-d "grant_type=refresh_token" \
-d "refresh_token=REFRESH_TOKEN"Get User's Account Information
After successfully authenticating with KonPro, you can retrieve detailed account information using the following endpoint:
curl -X GET https://api.konpro.ai/v1/pacific/account.get \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"Replace YOUR_ACCESS_TOKEN with the access token obtained during the OAuth process.
The response includes detailed information about the user's account, experiments, features, and subscription plan.
- Experiments: A list of feature experiments and their status for the user.
- Features: Enabled features for the user's account.
- Space Info: Information about the user's workspace, including permissions and roles.
- User: Basic user information, including email, registration date, and subscription status.
- User Plan: Detailed information about the user's subscription plan, including quotas, tier, and expiration.
Best Practices and Security Considerations
- Always use HTTPS for all OAuth-related requests.
- Store tokens securely and never expose them client-side.
- Implement token rotation and regularly refresh access tokens. If a request fails, check if the access token has expired.
- Validate the state parameter to prevent CSRF attacks.
- Use short-lived access tokens and long-lived refresh tokens.
- Implement proper error handling for token expiration and other OAuth-related errors.
Conclusion
This guide provides a comprehensive approach to integrating KonPro's API using OAuth 2.0 with the Authorization Code Flow and PKCE. By following these steps and best practices, you can securely authenticate users and access KonPro's API on their behalf.