Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(SQL Server) Upload Media to Twitter

Demonstrates uploading image or video files to Twitter. After uploading, the media_id can be used to attach the uploaded media to a tweet.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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-2019 Chilkat Software, Inc. All Rights Reserved.