Chattz OAuth allows you to authenticate users on your website or application using their Chattz account. This system follows the standard OAuth 2.0 flow, where users authorize your app, and you receive an access token to fetch user information.
Create an OAuth Client
Before integrating OAuth, you must create a client in your Chattz account:
- Go to OAuth Clients on Chattz.
- Provide:
- Name: A label for your own reference like “My Website Login”
- Redirect URI: The URL on your site where users will be sent after authentication.
- Click Add Client. You will get a Client ID and Client Secret.
Build the Authorization URL
To start the OAuth flow, redirect the user to the following URL:
https://chattz.net/oauth/authorize?response_type=code&client_id={client_id}&redirect_uri={redirect_uri}&state={state}Parameters:
- response_type: Must be
code. - client_id: Your client’s ID from Chattz.
- redirect_uri: Must exactly match the URI set in your client settings.
- state: Optional string to maintain state between request and callback (recommended for CSRF protection).
Example URL:
https://chattz.net/oauth/authorize?response_type=code&client_id=abc123&redirect_uri=https://example.com/chattz-callback&state=xyzHandle the Callback
After the user authorizes your app, Chattz will redirect them back to your redirect_uri with a code and the optional state parameter:
https://example.com/chattz-callback?code={authorization_code}&state={state}Exchange the Code for an Access Token
Send a POST request to exchange the code for an access token:
POST https://chattz.net/oauth/tokenBody Parameters:
- grant_type:
authorization_code - code: The authorization code received in the callback.
- client_id: Your client ID.
- client_secret: Your client secret.
- redirect_uri: Same redirect URI as used before.
Example Request (JavaScript / fetch):
async function getAccessToken(code) {
const response = await fetch('https://chattz.net/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
code: code,
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
redirect_uri: 'https://example.com/chattz-callback'
})
});
const data = await response.json();
if (!data.access_token) throw new Error('Failed to get access token');
return data.access_token;
}Example Response:
{
"access_token": "abcdef1234567890",
"token_type": "Bearer",
"expires_in": 3600
}Fetch User Information
Once you have the access token, use it to fetch the authenticated user’s info:
GET https://chattz.net/oauth/userinfoHeaders:
Authorization: Bearer {access_token}Example Request:
async function getUserInfo(token) {
const response = await fetch('https://chattz.net/oauth/userinfo', {
headers: {
'Authorization': `Bearer ${token}`
}
});
const user = await response.json();
console.log(user);
}Example Response:
{
"id": 123,
"username": "johndoe",
"email": "johndoe@example.com",
"first_name": "John",
"last_name": "Doe",
"avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..."
}Common Use Cases
- Authenticate users on your site using Chattz.
- Automatically create or link accounts in your system.
- Fetch user profile info, including first and last name, email, and avatar.
