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
(PowerBuilder) 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.
integer li_rc oleobject loo_JsonToken integer li_Success oleobject loo_Http oleobject loo_Req oleobject loo_Fac oleobject loo_Resp oleobject loo_JsonResponse // 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.. loo_JsonToken = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_JsonToken MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_JsonToken.LoadFile("qa_data/tokens/twitter.json") loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") // Provide the OAuth 1.0a credentials: loo_Http.OAuth1 = 1 loo_Http.OAuthConsumerKey = "TWITTER_CONSUMER_KEY" loo_Http.OAuthConsumerSecret = "TWITTER_CONSUMER_SECRET" loo_Http.OAuthToken = loo_JsonToken.StringOf("oauth_token") loo_Http.OAuthTokenSecret = loo_JsonToken.StringOf("oauth_token_secret") loo_Req = create oleobject // Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0 li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest") loo_Req.HttpVerb = "POST" loo_Req.ContentType = "multipart/form-data" loo_Req.Path = "/1.1/media/upload.json" loo_Req.AddHeader("Expect","100-continue") // Add a JPEG image file to the upload. loo_Fac = create oleobject // Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess") loo_JpgBytes = loo_Fac.ReadEntireFile("qa_data/jpg/starfish.jpg") loo_Req.AddBytesForUpload("media","starfish.jpg",loo_JpgBytes) loo_Resp = loo_Http.SynchronousRequest("upload.twitter.com",443,1,loo_Req) if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_JsonToken destroy loo_Http destroy loo_Req destroy loo_Fac return end if loo_JsonResponse = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat.JsonObject") loo_JsonResponse.EmitCompact = 0 loo_JsonResponse.Load(loo_Resp.BodyStr) if loo_Resp.StatusCode <> 200 then Write-Debug loo_JsonResponse.Emit() destroy loo_JsonToken destroy loo_Http destroy loo_Req destroy loo_Fac destroy loo_JsonResponse return end if destroy loo_Resp // Show the successful response: Write-Debug loo_JsonResponse.Emit() Write-Debug "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: Write-Debug "media_id_string = " + loo_JsonResponse.StringOf("media_id_string") Write-Debug "size = " + string(loo_JsonResponse.IntOf("size")) Write-Debug "image_type = " + loo_JsonResponse.StringOf("image.image_type") Write-Debug "height/width = " + string(loo_JsonResponse.IntOf("image.w")) + "," + string(loo_JsonResponse.IntOf("image.h")) destroy loo_JsonToken destroy loo_Http destroy loo_Req destroy loo_Fac destroy loo_JsonResponse |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.