Tcl Requires Chilkat v11.0.0+
Tcl
Twitter - Application Only Authentication
Demonstrates how to get a Twitter OAuth2 token for application-only authentication.This example is deprecated and no longer valid.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# It requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
# Note: Define your Twitter apps at https://apps.twitter.com/
# We're going to send an HTTPS POST that looks like this:
# POST /oauth2/token HTTP/1.1
# Host: api.twitter.com
# User-Agent: My Twitter App v1.0.23
# Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
# NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
# Content-Type: application/x-www-form-urlencoded;charset=UTF-8
# Content-Length: 29
# Accept-Encoding: gzip
#
# grant_type=client_credentials
# Update these values with your consumer key and secret (also known as API key and API secret).
set consumerKey "TWITTER_CONSUMER_KEY"
set consumerSecret "TWITTER_CONSUMER_SECRET"
set http [new_CkHttp]
CkHttp_put_BasicAuth $http 1
CkHttp_put_Login $http $consumerKey
CkHttp_put_Password $http $consumerSecret
set req [new_CkHttpRequest]
CkHttpRequest_AddParam $req "grant_type" "client_credentials"
CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
set resp [new_CkHttpResponse]
set success [CkHttp_HttpReq $http "https://api.twitter.com/oauth2/token" $req $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
puts "Response Status Code: [CkHttpResponse_get_StatusCode $resp]"
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
puts "Expected a 200 response status code for success."
puts [CkHttpResponse_bodyStr $resp]
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
# We should get a JSON response like this:
# {
# "token_type":"bearer",
# "access_token":"..."
# }
set json [new_CkJsonObject]
CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
puts "Access token = [CkJsonObject_stringOf $json access_token]"
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $json