Sample code for 30+ languages & platforms
SQL Server

Facebook Upload Photo from Local File

See more Facebook Examples

Uploads a new photo with a caption from a local file.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes Chilkat HTTP to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @req int
    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', '/v2.7/me/photos'

    -- Use the previously obtained access token.
    -- Note: The publish_actions scope is required to upload a photo.
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer FACEBOOK-ACCESS-TOKEN'

    -- Send an "Expect: 100-continue" header in the request.
    -- This causes the HTTP server to end a 100-continue response
    -- immediately after receiving the HTTP header.  The client
    -- (Chilkat) will receive this intermediate response, and if
    -- it's not an error response, it knows that the HTTP server will
    -- accept the data that is forthcoming.
    -- The alternative is to get an error response after trying to upload
    -- the entire contents of the files.
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue'

    -- Set a photo caption.
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'caption', 'Here are some penguins!'

    -- Additional parameters can be added to the request by 
    -- calling AddParam once per parameter.  
    -- See https://developers.facebook.com/docs/graph-api/reference/user/photos
    -- for more information about other optional parameters.

    -- Indicate the local file to be uploaded.
    EXEC sp_OAMethod @req, 'AddFileForUpload', @success OUT, 'source', 'qa_data/jpg/penguins2.jpg'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @req, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        RETURN
      END

    -- Upload the image file..
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'graph.facebook.com', 443, 1, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- Examine the response body:
    DECLARE @responseJson nvarchar(4000)
    EXEC sp_OAGetProperty @resp, 'BodyStr', @responseJson OUT

    PRINT @responseJson

    -- The response contains JSON, such as this:
    -- {"id":"10210224963435862","post_id":"10224048320139890_10210224963435862"}

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp


END
GO