React Native
React Native
Google Search Console API - Add
See more Google Search Console Examples
Demonstrates how to add a site to the set of the user's sites in Search Console.Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject, StringBuilder } 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.
// This example uses a previously obtained access token having permission for the
// Google Search Console scope.
// In this example, Get a Google Search Console OAuth2 Access Token, the access
// token was saved to a JSON file. This example fetches the access token from the file..
const jsonToken = new JsonObject();
jsonToken.loadFile('qa_data/tokens/googleSearchConsole.json');
if (!jsonToken.hasMember('access_token')) {
console.log('No access token found.');
return;
}
const http = new Http();
http.authToken = jsonToken.stringOf('access_token');
const sbUrlToAdd = new StringBuilder();
sbUrlToAdd.append('https://www.example.com/');
sbUrlToAdd.encode('url', 'utf-8');
const sbUrl = new StringBuilder();
sbUrl.append('https://www.googleapis.com/webmasters/v3/sites/');
sbUrl.appendSb(sbUrlToAdd);
// Our URL looks like this: https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F
console.log(sbUrl.getAsString());
const url = sbUrl.getAsString();
const resp = new HttpResponse();
try {
await http.httpNoBodyAsync('PUT', url, resp);
} catch {
console.log(http.lastErrorText);
return;
}
const statusCode = resp.statusCode;
console.log(`Response Status Code: ${statusCode}`);
if (statusCode >= 400) {
console.log(resp.bodyStr);
console.log('Failed.');
}
console.log('Success');
}