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
(C) 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.
#include <C_CkJsonObject.h> #include <C_CkHttp.h> #include <C_CkHttpRequest.h> #include <C_CkFileAccess.h> #include <C_CkByteData.h> #include <C_CkHttpResponse.h> void ChilkatSample(void) { HCkJsonObject jsonToken; BOOL success; HCkHttp http; HCkHttpRequest req; HCkFileAccess fac; HCkByteData jpgBytes; HCkHttpResponse resp; HCkJsonObject 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.. 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) { printf("%s\n",CkHttp_lastErrorText(http)); CkJsonObject_Dispose(jsonToken); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkFileAccess_Dispose(fac); CkByteData_Dispose(jpgBytes); return; } jsonResponse = CkJsonObject_Create(); CkJsonObject_putEmitCompact(jsonResponse,FALSE); CkJsonObject_Load(jsonResponse,CkHttpResponse_bodyStr(resp)); if (CkHttpResponse_getStatusCode(resp) != 200) { printf("%s\n",CkJsonObject_emit(jsonResponse)); CkJsonObject_Dispose(jsonToken); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkFileAccess_Dispose(fac); CkByteData_Dispose(jpgBytes); CkJsonObject_Dispose(jsonResponse); return; } CkHttpResponse_Dispose(resp); // Show the successful response: printf("%s\n",CkJsonObject_emit(jsonResponse)); printf("Success.\n"); // 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: printf("media_id_string = %s\n",CkJsonObject_stringOf(jsonResponse,"media_id_string")); printf("size = %d\n",CkJsonObject_IntOf(jsonResponse,"size")); printf("image_type = %s\n",CkJsonObject_stringOf(jsonResponse,"image.image_type")); printf("height/width = %d,%d\n",CkJsonObject_IntOf(jsonResponse,"image.w"),CkJsonObject_IntOf(jsonResponse,"image.h")); CkJsonObject_Dispose(jsonToken); CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkFileAccess_Dispose(fac); CkByteData_Dispose(jpgBytes); CkJsonObject_Dispose(jsonResponse); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.