Lua
Lua
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local rest = chilkat.Rest.new()
local bTls = true
local bAutoReconnect = true
local success = rest:connect("example.com", 443, bTls, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
-- Configure Google service-account authentication. Load the service account JSON key and set the
-- required OAuth scope(s).
local gAuth = chilkat.AuthGoogle.new()
local sbJsonKey = chilkat.StringBuilder.new()
local bLoaded = sbJsonKey:load_file("qa_data/service_account.json", "utf-8")
if not bLoaded then
print("Failed to load the service account JSON key.")
return
end
local jsonKey = sbJsonKey:get_as_string()
if not sbJsonKey.last_method_success then
print(sbJsonKey.last_error_text)
return
end
gAuth.json_key = jsonKey
-- A space-delimited list of the requested permissions.
gAuth.scope = "https://www.googleapis.com/auth/cloud-platform"
-- Associate the provider so Chilkat supplies a Google bearer token on each request.
success = rest:set_auth_google(gAuth)
if not success then
print(rest.last_error_text)
return
end
local responseText = rest:full_request_no_body("GET", "/storage/v1/b?project=my-project")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
print(responseText)