Refresh an Access token using the Refresh token to continue making calls on Peoplevine APIs.
Refresh a token via the API
The Refresh token is used to reactivate the Access token once the Access token’s 30 minute time limit expires. The Access token can be refreshed for 24 hours if the remember_me parameter was set to false in the call that created the Access and Refresh tokens, or for 7 days if the remember_me parameter was set to true.
Refreshing a token requires a call to the /api/token/refresh API containing your current access_token and refresh_token data. An example of this call is as follows:
Once a token has been successfully refreshed, it can continue to be used to access Peoplevine APIs.
Request and refresh a token in your code
You can both request and refresh a token programmatically in your app code to reduce the need for manual interaction with the APIs. Click the tab below matching your app language for example code snippets. Make sure to replace the following text with your company's information to make the code work as expected.
username
password
company_id
// Define the base URL for the API
const baseUrl = 'https://api.peoplevine.com/api/token';
// Define the interface for the user authentication response
interface UserAuthResponse {
refresh_token: string;
access_token: string;
}
async function authenticate(): Promise<void> {
try {
// Define the JSON payload for user authentication
const userAuthPayload = {
username: 'example@example.com',
password: '****',
grant_type: 'password',
remember_me: true,
};
// Send the user authentication request
const userAuthResponse = await fetch(baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userAuthPayload),
});
if (!userAuthResponse.ok) {
throw new Error('User authentication request failed');
}
const { refresh_token, access_token } = await userAuthResponse.json() as UserAuthResponse;
// Define the JSON payload for company authentication
const companyAuthPayload = {
refresh_token,
access_token,
grant_type: 'access_token',
company_id: 1234567,
};
// Send the company authentication request
const companyAuthResponse = await fetch(baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(companyAuthPayload),
});
if (!companyAuthResponse.ok) {
throw new Error('Company authentication request failed');
}
const companyAuthResult = await companyAuthResponse.json();
// Handle the response from the company authentication request
console.log(companyAuthResult);
} catch (error) {
// Handle any errors that occurred during the requests
console.error('An error occurred:', error);
}
}
// Call the authenticate function
authenticate();
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
await Authenticate();
}
static async Task Authenticate()
{
try
{
// Define the base URL for the API
string baseUrl = "https://api.peoplevine.com/api/token";
// Create an HttpClient instance
using var client = new HttpClient();
// Define the JSON payload for user authentication
var userAuthPayload = new
{
username = "example@example.com",
password = "****",
grant_type = "password",
remember_me = true
};
// Serialize the payload to JSON
string userAuthJson = JsonSerializer.Serialize(userAuthPayload);
// Send the user authentication request
HttpResponseMessage userAuthResponse = await client.PostAsync(baseUrl, new StringContent(userAuthJson, Encoding.UTF8, "application/json"));
userAuthResponse.EnsureSuccessStatusCode();
// Deserialize the response content to a UserAuthResponse object
string userAuthContent = await userAuthResponse.Content.ReadAsStringAsync();
var userAuthResult = JsonSerializer.Deserialize<UserAuthResponse>(userAuthContent);
// Define the JSON payload for company authentication
var companyAuthPayload = new
{
refresh_token = userAuthResult.RefreshToken,
access_token = userAuthResult.AccessToken,
grant_type = "access_token",
company_id = 1234567
};
// Serialize the payload to JSON
string companyAuthJson = JsonSerializer.Serialize(companyAuthPayload);
// Send the company authentication request
HttpResponseMessage companyAuthResponse = await client.PostAsync(baseUrl, new StringContent(companyAuthJson, Encoding.UTF8, "application/json"));
companyAuthResponse.EnsureSuccessStatusCode();
// Handle the response from the company authentication request
string companyAuthContent = await companyAuthResponse.Content.ReadAsStringAsync();
Console.WriteLine(companyAuthContent);
}
catch (Exception ex)
{
// Handle any errors that occurred during the requests
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
// Define the UserAuthResponse class for deserialization
class UserAuthResponse
{
public string RefreshToken { get; set; }
public string AccessToken { get; set; }
}
This will automatically create the User Access token, then create the User Company Access token. It will also refresh a token automatically when the Access token expires.