Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Web API Examples

Primary Categories

AWS Translate
Azure Maps
Azure OAuth2
Azure Storage Accounts
CardConnect
Cloudfare
Facebook
GeoOp
Jira
Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans

PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Walmart
Xero
eBay
effectconnect

 

 

 

(Tcl) Upload Media to Twitter

Demonstrates uploading image or video files to Twitter. After uploading, the media_id can be used to attach the uploaded media to a tweet.

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# It requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

# Assume we've previously obtained an access token and saved it to a JSON file..
set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/twitter.json"]

set http [new_CkHttp]

# Provide the OAuth 1.0a credentials:
CkHttp_put_OAuth1 $http 1
CkHttp_put_OAuthConsumerKey $http "TWITTER_CONSUMER_KEY"
CkHttp_put_OAuthConsumerSecret $http "TWITTER_CONSUMER_SECRET"
CkHttp_put_OAuthToken $http [CkJsonObject_stringOf $jsonToken "oauth_token"]
CkHttp_put_OAuthTokenSecret $http [CkJsonObject_stringOf $jsonToken "oauth_token_secret"]

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "multipart/form-data"
CkHttpRequest_put_Path $req "/1.1/media/upload.json"

CkHttpRequest_AddHeader $req "Expect" "100-continue"

# Add a JPEG image file to the upload.
set fac [new_CkFileAccess]

set jpgBytes [new_CkByteData]

set success [CkFileAccess_ReadEntireFile $fac "qa_data/jpg/starfish.jpg" $jpgBytes]
CkHttpRequest_AddBytesForUpload $req "media" "starfish.jpg" $jpgBytes

# resp is a CkHttpResponse
set resp [CkHttp_SynchronousRequest $http "upload.twitter.com" 443 1 $req]
if {[expr [CkHttp_get_LastMethodSuccess $http] != 1]} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkJsonObject $jsonToken
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkFileAccess $fac
    delete_CkByteData $jpgBytes
    exit
}

set jsonResponse [new_CkJsonObject]

CkJsonObject_put_EmitCompact $jsonResponse 0
CkJsonObject_Load $jsonResponse [CkHttpResponse_bodyStr $resp]

if {[expr [CkHttpResponse_get_StatusCode $resp] != 200]} then {
    puts [CkJsonObject_emit $jsonResponse]
    delete_CkJsonObject $jsonToken
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkFileAccess $fac
    delete_CkByteData $jpgBytes
    delete_CkJsonObject $jsonResponse
    exit
}

delete_CkHttpResponse $resp

# Show the successful response:
puts [CkJsonObject_emit $jsonResponse]
puts "Success."

# A successful JSON response looks like this:

# 	{
# 	  "media_id": 793137045996646400,
# 	  "media_id_string": "793137045996646400",
# 	  "size": 6229,
# 	  "expires_after_secs": 86400,
# 	  "image": {
# 	    "image_type": "image\/jpeg",
# 	    "w": 120,
# 	    "h": 120
# 	  }
# 	}
# 

# Get the information from the JSON:
puts "media_id_string = [CkJsonObject_stringOf $jsonResponse media_id_string]"
puts "size = [CkJsonObject_IntOf $jsonResponse size]"
puts "image_type = [CkJsonObject_stringOf $jsonResponse image.image_type]"
puts "height/width = [CkJsonObject_IntOf $jsonResponse image.w],[CkJsonObject_IntOf $jsonResponse image.h]"

delete_CkJsonObject $jsonToken
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkFileAccess $fac
delete_CkByteData $jpgBytes
delete_CkJsonObject $jsonResponse

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.