Sample code for 30+ languages & platforms
SQL Server

Get the Photos for a User

See more Facebook Examples

Demonstrates how to get the photos that the user has uploaded.

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
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @iTmp2 int
    DECLARE @iTmp3 int
    DECLARE @iTmp4 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 requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- This example assumes a previously obtained an access token
    DECLARE @oauth2 int
    EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth2 OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @oauth2, 'AccessToken', 'FACEBOOK-ACCESS-TOKEN'

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    -- Connect to Facebook.
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'graph.facebook.com', 443, 1, 1
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    -- Provide the authentication credentials (i.e. the access key)
    EXEC sp_OAMethod @rest, 'SetAuthOAuth2', @success OUT, @oauth2

    -- Indicate that we only want the photos the user has personally uploaded.
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'type', 'uploaded'

    -- We could limit the number of photos by setting a limit.
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'limit', '5'

    -- Gets the 1st page of photos. (Not the actual image data, but the information about each photo.)
    -- See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
    DECLARE @responseJson nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseJson OUT, 'GET', '/v2.7/me/photos'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Load', @success OUT, @responseJson
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- A sample JSON response is shown below.  
    -- This is the code to parse the JSON response.

    DECLARE @dtime int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dtime OUT

    DECLARE @bLocalTime int
    SELECT @bLocalTime = 1

    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.DtObj', @dt OUT

    DECLARE @i int
    SELECT @i = 0
    DECLARE @numItems int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numItems OUT, 'data'
    WHILE @i < @numItems
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i

        PRINT '--- ' + @i
        DECLARE @name nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'data[i].name'
        EXEC sp_OAGetProperty @json, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 = 1
          BEGIN

            PRINT 'name: ' + @name
          END

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'data[i].id'
        PRINT 'id: ' + @sTmp0

        -- We can load the created_time into a CkDateTime...
        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'data[i].created_time'
        EXEC sp_OAMethod @dtime, 'SetFromTimestamp', @success OUT, @sTmp0
        EXEC sp_OAMethod @dtime, 'ToDtObj', NULL, @bLocalTime, @dt

        EXEC sp_OAGetProperty @dt, 'Month', @iTmp0 OUT

        EXEC sp_OAGetProperty @dt, 'Day', @iTmp1 OUT

        EXEC sp_OAGetProperty @dt, 'Year', @iTmp2 OUT

        EXEC sp_OAGetProperty @dt, 'Hour', @iTmp3 OUT

        EXEC sp_OAGetProperty @dt, 'Minute', @iTmp4 OUT
        PRINT @iTmp0 + '/' + @iTmp1 + '/' + @iTmp2 + '  ' + @iTmp3 + ':' + @iTmp4
        SELECT @i = @i + 1
      END

    -- We can get the paging information as follows:

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'paging.next'
    PRINT 'URL for next page: ' + @sTmp0

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'paging.cursors.before'
    PRINT 'before cursor: ' + @sTmp0

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'paging.cursors.after'
    PRINT 'after cursor: ' + @sTmp0

    -- This is a sample JSON response:
    -- { 
    --   "data": [
    --     {
    --       "created_time": "2016-09-29T20:46:18+0000",
    --       "name": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
    --       "id": "10210199026347451"
    --     },
    --     { 
    --       "created_time": "2016-09-19T02:00:42+0000",
    --       "id": "10210091531240138"
    --     },
    --     { 
    --       "created_time": "2016-09-19T02:00:42+0000",
    --       "id": "10210091520620125"
    --     },
    --     { 
    --       "created_time": "2016-09-19T01:59:46+0000",
    --       "name": "I would've went for a swim had it not been for the sign",
    --       "id": "10210091522299917"
    --     },
    --     { 
    --       "created_time": "2016-09-12T00:37:35+0000",
    --       "id": "10210023316834798"
    --     }
    --   ],
    --   "paging": { 
    --     "cursors": { 
    --       "before": "MTAyMTAxOTkwMjYzNDc0NTEZD",
    --       "after": "MTAyMTAwMjMzMTU4MzQ3OTgZD"
    --     },
    --     "next": "https:\/\/graph.facebook.com\/v2.7\/10224048320139890\/photos?type=uploaded&limit=5&after=MTAyMTAwMjMzMTU4MzQ3OTgZD"
    --   }
    -- }
    -- 

    EXEC @hr = sp_OADestroy @oauth2
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @dtime
    EXEC @hr = sp_OADestroy @dt


END
GO