Chilkat Online Tools

SQL Server / Twitter API v2 / Retrieve Rules

Back to Collection Items

-- 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)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    DECLARE @success int

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://api.twitter.com/2/tweets/search/stream/rules', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "data": [
    --     {
    --       "id": "1273636687768285186",
    --       "value": "meme has:images"
    --     },
    --     {
    --       "id": "1273636687768285187",
    --       "value": "puppy has:media",
    --       "tag": "puppies with media"
    --     }
    --   ],
    --   "meta": {
    --     "sent": "2020-06-18T15:21:58.638Z"
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @id nvarchar(4000)

    DECLARE @value nvarchar(4000)

    DECLARE @tag nvarchar(4000)

    DECLARE @Sent nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Sent OUT, 'meta.sent'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'data[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @value OUT, 'data[i].value'
        EXEC sp_OAMethod @jResp, 'StringOf', @tag OUT, 'data[i].tag'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X GET
	-H "Authorization: Bearer <access_token>"
https://api.twitter.com/2/tweets/search/stream/rules

Postman Collection Item JSON

{
  "name": "Retrieve Rules",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.twitter.com/2/tweets/search/stream/rules",
      "protocol": "https",
      "host": [
        "api",
        "twitter",
        "com"
      ],
      "path": [
        "2",
        "tweets",
        "search",
        "stream",
        "rules"
      ]
    },
    "description": "Returns a list of filter rules currently active on the streaming endpoint.\n\n[Sign up](https://t.co/signup) for the Twitter API"
  },
  "response": [
    {
      "name": "Success 200 - Retrieve Rules",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/tweets/search/stream/rules",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "tweets",
            "search",
            "stream",
            "rules"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "content-disposition",
          "value": "attachment; filename=json.json"
        },
        {
          "key": "content-encoding",
          "value": "gzip"
        },
        {
          "key": "content-length",
          "value": "158"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Thu, 18 Jun 2020 15:21:58 GMT"
        },
        {
          "key": "server",
          "value": "tsa_b"
        },
        {
          "key": "strict-transport-security",
          "value": "max-age=631138519"
        },
        {
          "key": "x-access-level",
          "value": "read"
        },
        {
          "key": "x-connection-hash",
          "value": "d3ecb7f3763b04c3289e94d8bdbbf298"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "1800"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "1799"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1592494618"
        },
        {
          "key": "x-response-time",
          "value": "276"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": [\n        {\n            \"id\": \"1273636687768285186\",\n            \"value\": \"meme has:images\"\n        },\n        {\n            \"id\": \"1273636687768285187\",\n            \"value\": \"puppy has:media\",\n            \"tag\": \"puppies with media\"\n        }\n    ],\n    \"meta\": {\n        \"sent\": \"2020-06-18T15:21:58.638Z\"\n    }\n}"
    },
    {
      "name": "403 Forbidden - Client Not Enrolled",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/tweets/search/stream/rules",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "tweets",
            "search",
            "stream",
            "rules"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "content-disposition",
          "value": "attachment; filename=json.json"
        },
        {
          "key": "content-encoding",
          "value": "gzip"
        },
        {
          "key": "content-length",
          "value": "275"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Thu, 18 Jun 2020 15:25:53 GMT"
        },
        {
          "key": "server",
          "value": "tsa_b"
        },
        {
          "key": "strict-transport-security",
          "value": "max-age=631138519"
        },
        {
          "key": "x-access-level",
          "value": "read"
        },
        {
          "key": "x-connection-hash",
          "value": "d3ecb7f3763b04c3289e94d8bdbbf298"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-response-time",
          "value": "300"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"client_id\": \"16340226\",\n    \"required_enrollment\": \"Standard Basic\",\n    \"registration_url\": \"https://developer.twitter.com/en/account\",\n    \"title\": \"Client Forbidden\",\n    \"detail\": \"This request must be made using an approved developer account that is enrolled in the requested endpoint. Learn more by visiting our documentation.\",\n    \"reason\": \"client-not-enrolled\",\n    \"type\": \"https://api.twitter.com/2/problems/client-forbidden\"\n}"
    },
    {
      "name": "401 Unauthorized",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/tweets/search/stream/rules",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "tweets",
            "search",
            "stream",
            "rules"
          ]
        }
      },
      "status": "Authorization Required",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "content-disposition",
          "value": "attachment; filename=json.json"
        },
        {
          "key": "content-encoding",
          "value": "gzip"
        },
        {
          "key": "content-length",
          "value": "91"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Thu, 18 Jun 2020 15:25:01 GMT"
        },
        {
          "key": "server",
          "value": "tsa_b"
        },
        {
          "key": "strict-transport-security",
          "value": "max-age=631138519"
        },
        {
          "key": "x-connection-hash",
          "value": "d3ecb7f3763b04c3289e94d8bdbbf298"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-response-time",
          "value": "265"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"title\": \"Unauthorized\",\n    \"type\": \"about:blank\",\n    \"status\": 401,\n    \"detail\": \"Unauthorized\"\n}"
    }
  ]
}