Lua
Lua
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 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 sbUrlToAdd = chilkat.StringBuilder.new()
sbUrlToAdd:append("https://www.example.com/")
sbUrlToAdd:encode("url", "utf-8")
local sbUrl = chilkat.StringBuilder.new()
sbUrl:append("https://www.googleapis.com/webmasters/v3/sites/")
sbUrl:append_sb(sbUrlToAdd)
-- Our URL looks like this: https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F
print(sbUrl:get_as_string())
local url = sbUrl:get_as_string()
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("PUT", url, resp)
if not success then
print(http.last_error_text)
return
end
local statusCode = resp.status_code
print("Response Status Code: " .. statusCode)
if statusCode >= 400 then
print(resp.body_str)
print("Failed.")
end
print("Success")