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
(Unicode 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_CkJsonObjectW.h> #include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkFileAccessW.h> #include <C_CkByteData.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkJsonObjectW jsonToken; BOOL success; HCkHttpW http; HCkHttpRequestW req; HCkFileAccessW fac; HCkByteData jpgBytes; HCkHttpResponseW resp; HCkJsonObjectW 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 = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/twitter.json"); http = CkHttpW_Create(); // Provide the OAuth 1.0a credentials: CkHttpW_putOAuth1(http,TRUE); CkHttpW_putOAuthConsumerKey(http,L"TWITTER_CONSUMER_KEY"); CkHttpW_putOAuthConsumerSecret(http,L"TWITTER_CONSUMER_SECRET"); CkHttpW_putOAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token")); CkHttpW_putOAuthTokenSecret(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token_secret")); req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putContentType(req,L"multipart/form-data"); CkHttpRequestW_putPath(req,L"/1.1/media/upload.json"); CkHttpRequestW_AddHeader(req,L"Expect",L"100-continue"); // Add a JPEG image file to the upload. fac = CkFileAccessW_Create(); jpgBytes = CkByteData_Create(); success = CkFileAccessW_ReadEntireFile(fac,L"qa_data/jpg/starfish.jpg",jpgBytes); CkHttpRequestW_AddBytesForUpload(req,L"media",L"starfish.jpg",jpgBytes); resp = CkHttpW_SynchronousRequest(http,L"upload.twitter.com",443,TRUE,req); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkFileAccessW_Dispose(fac); CkByteData_Dispose(jpgBytes); return; } jsonResponse = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(jsonResponse,FALSE); CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp)); if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkFileAccessW_Dispose(fac); CkByteData_Dispose(jpgBytes); CkJsonObjectW_Dispose(jsonResponse); return; } CkHttpResponseW_Dispose(resp); // Show the successful response: wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); wprintf(L"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: wprintf(L"media_id_string = %s\n",CkJsonObjectW_stringOf(jsonResponse,L"media_id_string")); wprintf(L"size = %d\n",CkJsonObjectW_IntOf(jsonResponse,L"size")); wprintf(L"image_type = %s\n",CkJsonObjectW_stringOf(jsonResponse,L"image.image_type")); wprintf(L"height/width = %d,%d\n",CkJsonObjectW_IntOf(jsonResponse,L"image.w"),CkJsonObjectW_IntOf(jsonResponse,L"image.h")); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkFileAccessW_Dispose(fac); CkByteData_Dispose(jpgBytes); CkJsonObjectW_Dispose(jsonResponse); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.