Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
let rest = CkoRest()!
var bTls: Bool = true
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "example.com", port: 443, tls: bTls, autoReconnect: bAutoReconnect)
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
let gAuth = CkoAuthGoogle()!
let sbJsonKey = CkoStringBuilder()!
var bLoaded: Bool = sbJsonKey.loadFile(path: "qa_data/service_account.json")
if bLoaded != true {
print("Failed to load the service account JSON key.")
return
}
var jsonKey: String? = sbJsonKey.getAsString()
if sbJsonKey.lastMethodSuccess == false {
print("\(sbJsonKey.lastErrorText!)")
return
}
gAuth.jsonKey = 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.setAuthGoogle(authProvider: gAuth)
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
var responseText: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/storage/v1/b?project=my-project")
ERROR: "=" expected
ERROR: Undefined variable(CHECK_LMS)
print("\(responseText!)")
}