Sample code for 30+ languages & platforms
React Native

Google Search Console API - List

See more Google Search Console Examples

Lists the user's Search Console sites.

Chilkat React Native Downloads

React Native
import { Http, 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.

  // 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');

  let responseStr: string;
  try {
    responseStr = await http.quickGetStrAsync('https://www.googleapis.com/webmasters/v3/sites');
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const statusCode = http.lastStatus;
  console.log(`Response Status Code: ${statusCode}`);

  // Sample response:

  // {
  //  "siteEntry": [
  //   {
  //    "siteUrl": "https://www.example.com/",
  //    "permissionLevel": "siteUnverifiedUser"
  //   },
  //   {
  //    "siteUrl": "http://www.chilkatsoft.com/",
  //    "permissionLevel": "siteOwner"
  //   }
  //  ]
  // }

  console.log(responseStr);

  // Use this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  const json = new JsonObject();

  json.load(responseStr);

  let siteUrl = '';
  let permissionLevel = '';

  let i = 0;
  const countI = json.sizeOfArray('siteEntry');
  while (i < countI) {
    json.i = i;
    siteUrl = json.stringOf('siteEntry[i].siteUrl');
    console.log(`siteUrl: ${siteUrl}`);
    permissionLevel = json.stringOf('siteEntry[i].permissionLevel');
    console.log(`permissionLevel: ${permissionLevel}`);
    i++;
  }
}