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
(Delphi DLL) 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.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, CkByteData, HttpResponse, FileAccess, JsonObject, HttpRequest; ... procedure TForm1.Button1Click(Sender: TObject); var jsonToken: HCkJsonObject; success: Boolean; http: HCkHttp; req: HCkHttpRequest; fac: HCkFileAccess; jpgBytes: HCkByteData; resp: HCkHttpResponse; jsonResponse: HCkJsonObject; begin // 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.. jsonToken := CkJsonObject_Create(); success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/twitter.json'); http := CkHttp_Create(); // Provide the OAuth 1.0a credentials: CkHttp_putOAuth1(http,True); CkHttp_putOAuthConsumerKey(http,'TWITTER_CONSUMER_KEY'); CkHttp_putOAuthConsumerSecret(http,'TWITTER_CONSUMER_SECRET'); CkHttp_putOAuthToken(http,CkJsonObject__stringOf(jsonToken,'oauth_token')); CkHttp_putOAuthTokenSecret(http,CkJsonObject__stringOf(jsonToken,'oauth_token_secret')); req := CkHttpRequest_Create(); CkHttpRequest_putHttpVerb(req,'POST'); CkHttpRequest_putContentType(req,'multipart/form-data'); CkHttpRequest_putPath(req,'/1.1/media/upload.json'); CkHttpRequest_AddHeader(req,'Expect','100-continue'); // Add a JPEG image file to the upload. fac := CkFileAccess_Create(); jpgBytes := CkByteData_Create(); success := CkFileAccess_ReadEntireFile(fac,'qa_data/jpg/starfish.jpg',jpgBytes); CkHttpRequest_AddBytesForUpload(req,'media','starfish.jpg',jpgBytes); resp := CkHttp_SynchronousRequest(http,'upload.twitter.com',443,True,req); if (CkHttp_getLastMethodSuccess(http) <> True) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; jsonResponse := CkJsonObject_Create(); CkJsonObject_putEmitCompact(jsonResponse,False); CkJsonObject_Load(jsonResponse,CkHttpResponse__bodyStr(resp)); if (CkHttpResponse_getStatusCode(resp) <> 200) then begin Memo1.Lines.Add(CkJsonObject__emit(jsonResponse)); Exit; end; CkHttpResponse_Dispose(resp); // Show the successful response: Memo1.Lines.Add(CkJsonObject__emit(jsonResponse)); Memo1.Lines.Add('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: Memo1.Lines.Add('media_id_string = ' + CkJsonObject__stringOf(jsonResponse,'media_id_string')); Memo1.Lines.Add('size = ' + IntToStr(CkJsonObject_IntOf(jsonResponse,'size'))); Memo1.Lines.Add('image_type = ' + CkJsonObject__stringOf(jsonResponse,'image.image_type')); Memo1.Lines.Add('height/width = ' + IntToStr(CkJsonObject_IntOf(jsonResponse,'image.w')) + ',' + IntToStr(CkJsonObject_IntOf(jsonResponse,'image.h'))); CkJsonObject_Dispose(jsonToken); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkFileAccess_Dispose(fac); CkByteData_Dispose(jpgBytes); CkJsonObject_Dispose(jsonResponse); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.