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
(DataFlex) 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.
Use ChilkatAx-win32.pkg Procedure Test Handle hoJsonToken Boolean iSuccess Handle hoHttp Variant vReq Handle hoReq Handle hoFac Variant hoJpgBytes Variant vResp Handle hoResp Handle hoJsonResponse String sTemp1 Integer iTemp1 Integer iTemp2 Boolean bTemp1 // 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.. Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken If (Not(IsComObjectCreated(hoJsonToken))) Begin Send CreateComObject of hoJsonToken End Get ComLoadFile Of hoJsonToken "qa_data/tokens/twitter.json" To iSuccess Get Create (RefClass(cComChilkatHttp)) To hoHttp If (Not(IsComObjectCreated(hoHttp))) Begin Send CreateComObject of hoHttp End // Provide the OAuth 1.0a credentials: Set ComOAuth1 Of hoHttp To True Set ComOAuthConsumerKey Of hoHttp To "TWITTER_CONSUMER_KEY" Set ComOAuthConsumerSecret Of hoHttp To "TWITTER_CONSUMER_SECRET" Get ComStringOf Of hoJsonToken "oauth_token" To sTemp1 Set ComOAuthToken Of hoHttp To sTemp1 Get ComStringOf Of hoJsonToken "oauth_token_secret" To sTemp1 Set ComOAuthTokenSecret Of hoHttp To sTemp1 Get Create (RefClass(cComChilkatHttpRequest)) To hoReq If (Not(IsComObjectCreated(hoReq))) Begin Send CreateComObject of hoReq End Set ComHttpVerb Of hoReq To "POST" Set ComContentType Of hoReq To "multipart/form-data" Set ComPath Of hoReq To "/1.1/media/upload.json" Send ComAddHeader To hoReq "Expect" "100-continue" // Add a JPEG image file to the upload. Get Create (RefClass(cComCkFileAccess)) To hoFac If (Not(IsComObjectCreated(hoFac))) Begin Send CreateComObject of hoFac End Get ComReadEntireFile Of hoFac "qa_data/jpg/starfish.jpg" To hoJpgBytes Get ComAddBytesForUpload Of hoReq "media" "starfish.jpg" vJpgBytes To iSuccess Get pvComObject of hoReq to vReq Get ComSynchronousRequest Of hoHttp "upload.twitter.com" 443 True vReq To vResp If (IsComObject(vResp)) Begin Get Create (RefClass(cComChilkatHttpResponse)) To hoResp Set pvComObject Of hoResp To vResp End Get ComLastMethodSuccess Of hoHttp To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoHttp To sTemp1 Showln sTemp1 Procedure_Return End Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse If (Not(IsComObjectCreated(hoJsonResponse))) Begin Send CreateComObject of hoJsonResponse End Set ComEmitCompact Of hoJsonResponse To False Get ComBodyStr Of hoResp To sTemp1 Get ComLoad Of hoJsonResponse sTemp1 To iSuccess Get ComStatusCode Of hoResp To iTemp1 If (iTemp1 <> 200) Begin Get ComEmit Of hoJsonResponse To sTemp1 Showln sTemp1 Procedure_Return End Send Destroy of hoResp // Show the successful response: Get ComEmit Of hoJsonResponse To sTemp1 Showln sTemp1 Showln "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: Get ComStringOf Of hoJsonResponse "media_id_string" To sTemp1 Showln "media_id_string = " sTemp1 Get ComIntOf Of hoJsonResponse "size" To iTemp1 Showln "size = " iTemp1 Get ComStringOf Of hoJsonResponse "image.image_type" To sTemp1 Showln "image_type = " sTemp1 Get ComIntOf Of hoJsonResponse "image.w" To iTemp1 Get ComIntOf Of hoJsonResponse "image.h" To iTemp2 Showln "height/width = " iTemp1 "," iTemp2 End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.