SQL Server
SQL Server
Facebook User Feed (Read Posts)
See more Facebook Examples
Demonstrates how to read the feed of posts (including status updates) and links published by this person, or by others on this person's profile. This introductory example gets the first few posts.Chilkat SQL Server Downloads
-- 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 and send the following GET request:
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
-- We could limit the number of posts. The default limit is 25.
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'limit', '5'
DECLARE @responseJson nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseJson OUT, 'GET', '/v2.7/me/feed'
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
-- Show the JSON in human-readable format.
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 @message nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @message OUT, 'data[i].message'
EXEC sp_OAGetProperty @json, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'message: ' + @message
END
DECLARE @story nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @story OUT, 'data[i].story'
EXEC sp_OAGetProperty @json, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'story: ' + @story
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
-- Finally, there are two paging URLs at the very bottom. Those can be accessed as shown:
DECLARE @prevUrl nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @prevUrl OUT, 'paging.previous'
PRINT 'URL for previous page: ' + @prevUrl
DECLARE @nextUrl nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @nextUrl OUT, 'paging.next'
PRINT 'URL for next page: ' + @nextUrl
-- -------------------------------------------------------------------
-- This is a sample JSON response..
-- {
-- "data": [
-- {
-- "message": "This is the first post.",
-- "created_time": "2016-09-28T14:36:45+0000",
-- "id": "12345678901234567_12345678900000001"
-- },
-- {
-- "message": "This is the 2nd post.",
-- "created_time": "2016-09-27T03:26:12+0000",
-- "id": "12345678901234567_12345678900000002"
-- },
-- {
-- "story": "John Doe shared Mary's post.",
-- "created_time": "2016-09-26T15:45:10+0000",
-- "id": "12345678901234567_12345678900000003"
-- },
-- {
-- "message": "https:\/\/www.somewebsite.org\/?ID=8158",
-- "created_time": "2016-09-26T02:29:20+0000",
-- "id": "12345678901234567_12345678900000004"
-- },
-- {
-- "message": "http:\/\/blog.somebody.com\/post\/123",
-- "created_time": "2016-09-26T02:02:56+0000",
-- "id": "12345678901234567_12345678900000005"
-- },
-- {
-- "story": "John Doe shared Bill's photo.",
-- "created_time": "2016-09-25T15:32:09+0000",
-- "id": "12345678901234567_12345678900000006"
-- },
-- {
-- "message": "Check this out...",
-- "story": "John Doe shared NPR Music's video.",
-- "created_time": "2016-09-25T14:54:52+0000",
-- "id": "12345678901234567_12345678900000007"
-- },
-- {
-- "message": "Yet another post...",
-- "created_time": "2016-09-24T17:21:30+0000",
-- "id": "12345678901234567_12345678900000008"
-- },
-- {
-- "message": "http:\/\/www.somewhere.com\/blahblahblah\/",
-- "created_time": "2016-09-23T22:20:29+0000",
-- "id": "12345678901234567_12345678900000009"
-- },
-- {
-- "message": "At Lincoln marsh ...",
-- "story": "John Doe added 2 new photos.",
-- "created_time": "2016-09-19T02:00:43+0000",
-- "id": "12345678901234567_12345678900000010"
-- },
-- {
-- "message": "I would've went for a swim had it not been for the sign",
-- "created_time": "2016-09-19T01:59:47+0000",
-- "id": "12345678901234567_12345678900000011"
-- },
-- {
-- "story": "John Doe shared Steve's video.",
-- "created_time": "2016-09-17T22:00:02+0000",
-- "id": "12345678901234567_12345678900000012"
-- },
-- {
-- "message": "Another sample message...",
-- "story": "John Doe shared XYZ's post.",
-- "created_time": "2016-09-17T14:13:16+0000",
-- "id": "12345678901234567_12345678900000013"
-- },
-- {
-- "message": "Hello World!",
-- "created_time": "2016-09-16T23:17:13+0000",
-- "id": "12345678901234567_12345678900000014"
-- },
-- {
-- "message": "https:\/\/www.youtube.com\/watch?v=xyz",
-- "created_time": "2016-09-15T04:13:02+0000",
-- "id": "12345678901234567_12345678900000015"
-- },
-- {
-- "story": "John Doe shared Emily's photo.",
-- "created_time": "2016-09-14T16:37:10+0000",
-- "id": "12345678901234567_12345678900000016"
-- },
-- {
-- "story": "John Doe shared Vincent's photo.",
-- "created_time": "2016-09-14T14:03:42+0000",
-- "id": "12345678901234567_12345678900000017"
-- },
-- {
-- "message": "https:\/\/www.chilkatsoft.com\/",
-- "created_time": "2016-09-14T02:29:37+0000",
-- "id": "12345678901234567_12345678900000018"
-- },
-- {
-- "message": "So cool! Marcus jamming with this 16 year old kid...",
-- "created_time": "2016-09-13T17:04:53+0000",
-- "id": "12345678901234567_12345678900000019"
-- },
-- {
-- "message": "Wow!",
-- "story": "John Doe shared Somebody's post.",
-- "created_time": "2016-09-13T14:11:18+0000",
-- "id": "12345678901234567_12345678900000020"
-- }
-- ],
-- "paging": {
-- "previous": "https:\/\/graph.facebook.com\/v2.7\/12345678901234567\/feed?since=1475073405&limit=25&__paging_token=enc_abcT9x9GFabcTxvnyXZC0xnHZCrH2h4yjNZCZAUabcZAI9TyZBibwat6wCYiSFTxR4nSDZAkzaN8vIaEPmqrabc1QTIbGac&__previous=1",
-- "next": "https:\/\/graph.facebook.com\/v2.7\/12345678901234567\/feed?limit=25&until=1473426350&__paging_token=enc_AdDFWabcYtV5ob6lFO8LZAMhm7LKCxntawqqabcZALyC8WZB8Th2lON7HGuJRabzW6Ivfm93LORIw9NNBuHpob7abcjh"
-- }
-- }
--
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