Lua
Lua
Google Search Console API - List
See more Google Search Console Examples
Lists the user's Search Console sites.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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..
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/googleSearchConsole.json")
if not jsonToken:has_member("access_token") then
print("No access token found.")
return
end
local http = chilkat.Http.new()
http.auth_token = jsonToken:string_of("access_token")
local responseStr = http:quick_get_str("https://www.googleapis.com/webmasters/v3/sites")
if not http.last_method_success then
print(http.last_error_text)
return
end
local statusCode = http.last_status
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
local json = chilkat.JsonObject.new()
success = json:load(responseStr)
local siteUrl = ""
local permissionLevel = ""
local i = 0
local count_i = json:size_of_array("siteEntry")
while i < count_i do
json.i = i
siteUrl = json:string_of("siteEntry[i].siteUrl")
print("siteUrl: " .. siteUrl)
permissionLevel = json:string_of("siteEntry[i].permissionLevel")
print("permissionLevel: " .. permissionLevel)
i = i + 1
end