React Native
React Native
Create a New GMail Label
See more GMail REST API Examples
Demonstrates how to create a new GMail label.Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
http.authToken = 'GMAIL-ACCESS-TOKEN';
const userId = 'me';
http.setUrlVar('userId', userId);
// Create the JSON to be sent in the HTTP request body.
// The name of the new label is "questions".
const json = new JsonObject();
json.updateString('name', 'questions');
json.updateString('labelListVisibility', 'labelShow');
json.updateString('messageListVisibility', 'show');
json.emitCompact = false;
console.log(json.emit());
// The JSON contains this:
// {
// "name": "questions",
// "labelListVisibility": "labelShow",
// "messageListVisibility": "show"
// }
const url = 'https://www.googleapis.com/gmail/v1/users/{$userId}/labels';
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', url, json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
console.log(`status = ${resp.statusCode}`);
// A 200 response status indicate success.
if (resp.statusCode !== 200) {
console.log(resp.bodyStr);
console.log('Failed.');
return;
}
// A successful repsonse contains JSON that looks like this:
// {
// "id": "Label_43",
// "name": "questions",
// "messageListVisibility": "show",
// "labelListVisibility": "labelShow"
// }
console.log('response body:');
console.log(resp.bodyStr);
console.log('GMail label created!');
}