Dart
Dart
Google Search Console API - List
See more Google Search Console Examples
Lists the user's Search Console sites.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// 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..
final jsonToken = CkJsonObject();
jsonToken.loadFile('qa_data/tokens/googleSearchConsole.json');
if (!jsonToken.hasMember('access_token')) {
print('No access token found.');
return;
}
final http = CkHttp();
http.authToken = jsonToken.stringOf('access_token');
final String responseStr;
try {
responseStr = http.quickGetStr('https://www.googleapis.com/webmasters/v3/sites');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final statusCode = http.lastStatus;
print('Response Status Code: $statusCode');
// Sample response:
// {
// "siteEntry": [
// {
// "siteUrl": "https://www.example.com/",
// "permissionLevel": "siteUnverifiedUser"
// },
// {
// "siteUrl": "http://www.chilkatsoft.com/",
// "permissionLevel": "siteOwner"
// }
// ]
// }
print(responseStr);
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
final json = CkJsonObject();
json.load(responseStr);
var siteUrl = '';
var permissionLevel = '';
var i = 0;
final countI = json.sizeOfArray('siteEntry');
while (i < countI) {
json.i = i;
siteUrl = json.stringOf('siteEntry[i].siteUrl');
print('siteUrl: $siteUrl');
permissionLevel = json.stringOf('siteEntry[i].permissionLevel');
print('permissionLevel: $permissionLevel');
i++;
}
}