Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Tcl) Upload Media to TwitterDemonstrates uploading image or video files to Twitter. After uploading, the media_id can be used to attach the uploaded media to a tweet.
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 {[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 {[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-2024 Chilkat Software, Inc. All Rights Reserved.