React Native
React Native
Get Populi Access Key (Token)
See more Populi Examples
Demonstrates how to authenticate and obtain an access key using a username and password.Chilkat React Native Downloads
import { Rest, Xml } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = new Rest();
// Connect using TLS.
// A single REST object, once connected, can be used for many Populi REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
const bAutoReconnect = true;
try {
await rest.connectAsync('yourcollege.populi.co', 443, true, bAutoReconnect);
} catch {
console.log(rest.lastErrorText);
return;
}
rest.addQueryParam('username', 'POPULI_USERNAME');
rest.addQueryParam('password', 'POPULI_PASSWORD');
let responseBody: string;
try {
responseBody = await rest.fullRequestFormUrlEncodedAsync('POST', '/api/index.php');
} catch {
console.log(rest.lastErrorText);
return;
}
// We should expect a 200 response if successful.
if (rest.responseStatusCode !== 200) {
console.log('Request Header: ');
console.log(rest.lastRequestHeader);
console.log('----');
console.log(`Response StatusCode = ${rest.responseStatusCode}`);
console.log(`Response StatusLine: ${rest.responseStatusText}`);
console.log('Response Header:');
console.log(rest.responseHeader);
console.log('Response Body:');
console.log(responseBody);
return;
}
const xml = new Xml();
xml.loadXml(responseBody);
console.log(xml.getXml());
// Sample response:
// <?xml version="1.0" encoding="ISO-8859-1"?>
// <response>
// <access_key>63481e36023b5d ... 76d0dbc314</access_key>
// <accountid>19999999</accountid>
// <accounttype>PERSON</accounttype>
// </response>
// Save the XML to a file for subsequent requests..
xml.saveXml('qa_data/tokens/populi_token.xml');
}