Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set rest [new_CkRest]

set bTls 1
set bAutoReconnect 1
set success [CkRest_Connect $rest "example.com" 443 $bTls $bAutoReconnect]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

#  Configure OAuth 1.0a request signing.  Provide the consumer credentials and the access token
#  credentials; Chilkat computes the OAuth signature for each request.
set oauth1 [new_CkOAuth1]

#  Normally you would not hard-code secrets in source.  You should instead obtain them from an
#  interactive prompt, environment variable, or a secrets vault.
CkOAuth1_put_ConsumerKey $oauth1 "CONSUMER_KEY"
CkOAuth1_put_ConsumerSecret $oauth1 "CONSUMER_SECRET"
CkOAuth1_put_Token $oauth1 "ACCESS_TOKEN"
CkOAuth1_put_TokenSecret $oauth1 "ACCESS_TOKEN_SECRET"
CkOAuth1_put_SignatureMethod $oauth1 "HMAC-SHA1"

#  The 2nd argument selects whether OAuth parameters are placed in the query string (1) or the
#  Authorization header (0).
set bUseQueryParams 0
set success [CkRest_SetAuthOAuth1 $rest $oauth1 $bUseQueryParams]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth1 $oauth1
    exit
}

set responseText [CkRest_fullRequestNoBody $rest "GET" "/api/resource"]
if {[CkRest_get_LastMethodSuccess $rest] == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth1 $oauth1
    exit
}

puts "$responseText"

delete_CkRest $rest
delete_CkOAuth1 $oauth1