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
(SQL Server) 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.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int DECLARE @iTmp1 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- 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.. DECLARE @jsonToken int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/twitter.json' DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT -- Provide the OAuth 1.0a credentials: EXEC sp_OASetProperty @http, 'OAuth1', 1 EXEC sp_OASetProperty @http, 'OAuthConsumerKey', 'TWITTER_CONSUMER_KEY' EXEC sp_OASetProperty @http, 'OAuthConsumerSecret', 'TWITTER_CONSUMER_SECRET' EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token' EXEC sp_OASetProperty @http, 'OAuthToken', @sTmp0 EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token_secret' EXEC sp_OASetProperty @http, 'OAuthTokenSecret', @sTmp0 DECLARE @req int -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT EXEC sp_OASetProperty @req, 'HttpVerb', 'POST' EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data' EXEC sp_OASetProperty @req, 'Path', '/1.1/media/upload.json' EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue' -- Add a JPEG image file to the upload. DECLARE @fac int -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT EXEC sp_OAMethod @fac, 'ReadEntireFile', @jpgBytes OUT, 'qa_data/jpg/starfish.jpg' EXEC sp_OAMethod @req, 'AddBytesForUpload', @success OUT, 'media', 'starfish.jpg', @jpgBytes DECLARE @resp int EXEC sp_OAMethod @http, 'SynchronousRequest', @resp OUT, 'upload.twitter.com', 443, 1, @req EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req EXEC @hr = sp_OADestroy @fac RETURN END DECLARE @jsonResponse int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResponse OUT EXEC sp_OASetProperty @jsonResponse, 'EmitCompact', 0 EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @sTmp0 EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT IF @iTmp0 <> 200 BEGIN EXEC sp_OAMethod @jsonResponse, 'Emit', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req EXEC @hr = sp_OADestroy @fac EXEC @hr = sp_OADestroy @jsonResponse RETURN END EXEC @hr = sp_OADestroy @resp -- Show the successful response: EXEC sp_OAMethod @jsonResponse, 'Emit', @sTmp0 OUT PRINT @sTmp0 PRINT '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: EXEC sp_OAMethod @jsonResponse, 'StringOf', @sTmp0 OUT, 'media_id_string' PRINT 'media_id_string = ' + @sTmp0 EXEC sp_OAMethod @jsonResponse, 'IntOf', @iTmp0 OUT, 'size' PRINT 'size = ' + @iTmp0 EXEC sp_OAMethod @jsonResponse, 'StringOf', @sTmp0 OUT, 'image.image_type' PRINT 'image_type = ' + @sTmp0 EXEC sp_OAMethod @jsonResponse, 'IntOf', @iTmp0 OUT, 'image.w' EXEC sp_OAMethod @jsonResponse, 'IntOf', @iTmp1 OUT, 'image.h' PRINT 'height/width = ' + @iTmp0 + ',' + @iTmp1 EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req EXEC @hr = sp_OADestroy @fac EXEC @hr = sp_OADestroy @jsonResponse END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.