Swift
Swift
Configure OAuth 1.0a Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.
Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.
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)
if success == false {
print("\(rest.lastErrorText!)")
return
}
// Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
// credentials; Chilkat computes the OAuth signature for each request.
let oauth1 = CkoOAuth1()!
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
oauth1.consumerKey = "CONSUMER_KEY"
oauth1.consumerSecret = "CONSUMER_SECRET"
oauth1.token = "ACCESS_TOKEN"
oauth1.tokenSecret = "ACCESS_TOKEN_SECRET"
oauth1.signatureMethod = "HMAC-SHA1"
// The 2nd argument selects whether OAuth parameters are placed in the query string (true) or the
// Authorization header (false).
var bUseQueryParams: Bool = false
success = rest.setAuthOAuth1(authProvider: oauth1, useQueryParams: bUseQueryParams)
if success == false {
print("\(rest.lastErrorText!)")
return
}
var responseText: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/api/resource")
if rest.lastMethodSuccess == false {
print("\(rest.lastErrorText!)")
return
}
print("\(responseText!)")
}