React Native
React Native
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat React Native Downloads
import { AuthGoogle, Rest, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
const rest = new Rest();
const bTls = true;
const bAutoReconnect = true;
try {
await rest.connectAsync('example.com', 443, bTls, bAutoReconnect);
} catch {
console.log(rest.lastErrorText);
return;
}
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
const gAuth = new AuthGoogle();
const sbJsonKey = new StringBuilder();
try {
sbJsonKey.loadFile('qa_data/service_account.json', 'utf-8');
} catch {
console.log('Failed to load the service account JSON key.');
return;
}
let jsonKey: string;
try {
jsonKey = sbJsonKey.getAsString();
} catch {
console.log(sbJsonKey.lastErrorText);
return;
}
gAuth.jsonKey = jsonKey;
// A space-delimited list of the requested permissions.
gAuth.scope = 'https://www.googleapis.com/auth/cloud-platform';
// Associate the provider so Chilkat supplies a Google bearer token on each request.
try {
rest.setAuthGoogle(gAuth);
} catch {
console.log(rest.lastErrorText);
return;
}
let responseText: string;
try {
responseText = await rest.fullRequestNoBodyAsync('GET', '/storage/v1/b?project=my-project');
} catch {
console.log(rest.lastErrorText);
return;
}
console.log(responseText);
}