Chilkat Online Tools

SQL Server / Twitter API v2 / User by ID

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/users/:id', @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)

    -- {
    --   "errors": [
    --     {
    --       "detail": "Could not find user with id: [2244994946].",
    --       "title": "Not Found Error",
    --       "resource_type": "user",
    --       "parameter": "id",
    --       "value": "2244994946",
    --       "type": "https://api.twitter.com/2/problems/resource-not-found"
    --     }
    --   ]
    -- }

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

    DECLARE @detail nvarchar(4000)

    DECLARE @title nvarchar(4000)

    DECLARE @resource_type nvarchar(4000)

    DECLARE @parameter nvarchar(4000)

    DECLARE @value nvarchar(4000)

    DECLARE @v_type nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'errors'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @detail OUT, 'errors[i].detail'
        EXEC sp_OAMethod @jResp, 'StringOf', @title OUT, 'errors[i].title'
        EXEC sp_OAMethod @jResp, 'StringOf', @resource_type OUT, 'errors[i].resource_type'
        EXEC sp_OAMethod @jResp, 'StringOf', @parameter OUT, 'errors[i].parameter'
        EXEC sp_OAMethod @jResp, 'StringOf', @value OUT, 'errors[i].value'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'errors[i].type'
        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/users/:id

Postman Collection Item JSON

{
  "name": "User by ID",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.twitter.com/2/users/:id",
      "protocol": "https",
      "host": [
        "api",
        "twitter",
        "com"
      ],
      "path": [
        "2",
        "users",
        ":id"
      ],
      "query": [
        {
          "key": "user.fields",
          "value": "",
          "description": "Comma-separated fields for the user object.\n\nAllowed values:\ncreated_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld\n\nDefault values:\nid,name,username",
          "disabled": true
        },
        {
          "key": "expansions",
          "value": "",
          "description": "Expansions enable requests to expand an ID into a full object in the includes response object.\n\nAllowed value:\npinned_tweet_id\n\nDefault value: none",
          "disabled": true
        },
        {
          "key": "tweet.fields",
          "value": "",
          "description": "Comma-separated list of fields for the Tweet object. Expansion required.\n\nAllowed values:\nattachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,reply_settings,source,text,withheld\n\nDefault values:\nid,text\n\nOAuth1.0a User Context authorization required if any of the following fields are included in the request:\nnon_public_metrics,organic_metrics,promoted_metrics",
          "disabled": true
        }
      ],
      "variable": [
        {
          "key": "id",
          "value": "",
          "type": "string",
          "description": "Required. Enter a single user ID."
        }
      ]
    },
    "description": "This endpoint returns details about a user by ID.\n\nFor full details, see the [API reference](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id) for this endpoint.\n\n[Sign up](https://t.co/signup) for the Twitter API"
  },
  "response": [
    {
      "name": "200 Success - User Not Found",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/users/:id",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "users",
            ":id"
          ],
          "query": [
            {
              "key": "user.fields",
              "value": "",
              "description": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld",
              "disabled": true
            },
            {
              "key": "expansions",
              "value": "",
              "description": "pinned_tweet_id",
              "disabled": true
            },
            {
              "key": "tweet.fields",
              "value": "",
              "description": "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,source,text,withheld",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "id",
              "value": "2244994946"
            }
          ]
        }
      },
      "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": "186"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Fri, 26 Jun 2020 16:22:36 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": "eae8bea11e85fe49e732de33763f0cf2"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "15"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "12"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1593189419"
        },
        {
          "key": "x-response-time",
          "value": "63"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"errors\": [\n        {\n            \"detail\": \"Could not find user with id: [2244994946].\",\n            \"title\": \"Not Found Error\",\n            \"resource_type\": \"user\",\n            \"parameter\": \"id\",\n            \"value\": \"2244994946\",\n            \"type\": \"https://api.twitter.com/2/problems/resource-not-found\"\n        }\n    ]\n}"
    },
    {
      "name": "429 Rate Limit Exceeded",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/users/:id",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "users",
            ":id"
          ],
          "query": [
            {
              "key": "user.fields",
              "value": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,url,username,verified,withheld",
              "description": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld",
              "disabled": true
            },
            {
              "key": "expansions",
              "value": "pinned_tweet_id",
              "description": "pinned_tweet_id",
              "disabled": true
            },
            {
              "key": "tweet.fields",
              "value": "attachments,author_id,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,referenced_tweets,source,text,withheld",
              "description": "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,source,text,withheld",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "id",
              "value": "2244994945"
            }
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "plain",
      "header": [
        {
          "key": "content-encoding",
          "value": "gzip"
        },
        {
          "key": "content-length",
          "value": "46"
        },
        {
          "key": "content-type",
          "value": "text/plain;charset=utf-8"
        },
        {
          "key": "date",
          "value": "Fri, 26 Jun 2020 15:34:56 GMT"
        },
        {
          "key": "server",
          "value": "tsa_b"
        },
        {
          "key": "strict-transport-security",
          "value": "max-age=631138519"
        },
        {
          "key": "x-connection-hash",
          "value": "bf6b8cccb0bc95bc56a50be4db9a2d09"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "15"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "0"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1593186139"
        },
        {
          "key": "x-response-time",
          "value": "6"
        }
      ],
      "cookie": [
      ],
      "body": "Rate limit exceeded\n"
    },
    {
      "name": "200 Success - Default Payload",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/users/:id",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "users",
            ":id"
          ],
          "query": [
            {
              "key": "user.fields",
              "value": "",
              "description": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld",
              "disabled": true
            },
            {
              "key": "expansions",
              "value": "",
              "description": "pinned_tweet_id",
              "disabled": true
            },
            {
              "key": "tweet.fields",
              "value": "",
              "description": "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,source,text,withheld",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "id",
              "value": "2244994945"
            }
          ]
        }
      },
      "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": "83"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Fri, 26 Jun 2020 16:21:59 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": "eae8bea11e85fe49e732de33763f0cf2"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "15"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "14"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1593189419"
        },
        {
          "key": "x-response-time",
          "value": "59"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": {\n        \"id\": \"2244994945\",\n        \"name\": \"Twitter Dev\",\n        \"username\": \"TwitterDev\"\n    }\n}"
    },
    {
      "name": "User by ID",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/users/:id",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "users",
            ":id"
          ],
          "query": [
            {
              "key": "user.fields",
              "value": "",
              "description": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld",
              "disabled": true
            },
            {
              "key": "expansions",
              "value": "",
              "description": "pinned_tweet_id",
              "disabled": true
            },
            {
              "key": "tweet.fields",
              "value": "",
              "description": "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,source,text,withheld",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "id",
              "value": ""
            }
          ]
        }
      },
      "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": "219"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Fri, 26 Jun 2020 16:25:31 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": "eae8bea11e85fe49e732de33763f0cf2"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "15"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "8"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1593189419"
        },
        {
          "key": "x-response-time",
          "value": "29"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": {\n        \"created_at\": \"2020-06-17T19:10:19.000Z\",\n        \"description\": \"\",\n        \"id\": \"1273332037970202626\",\n        \"name\": \"Clou\",\n        \"profile_image_url\": \"https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png\",\n        \"protected\": true,\n        \"url\": \"\",\n        \"username\": \"Clou18829992\",\n        \"verified\": false\n    }\n}"
    },
    {
      "name": "200 Success - Request Fields for User and Expanded Pinned Tweet",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.twitter.com/2/users/:id?user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,url,username,verified,withheld&expansions=pinned_tweet_id&tweet.fields=attachments,author_id,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,referenced_tweets,source,text,withheld",
          "protocol": "https",
          "host": [
            "api",
            "twitter",
            "com"
          ],
          "path": [
            "2",
            "users",
            ":id"
          ],
          "query": [
            {
              "key": "user.fields",
              "value": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,url,username,verified,withheld",
              "description": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld"
            },
            {
              "key": "expansions",
              "value": "pinned_tweet_id",
              "description": "pinned_tweet_id"
            },
            {
              "key": "tweet.fields",
              "value": "attachments,author_id,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,referenced_tweets,source,text,withheld",
              "description": "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,organic_metrics,possibly_sensitive,promoted_metrics,public_metrics,referenced_tweets,source,text,withheld"
            }
          ],
          "variable": [
            {
              "key": "id",
              "value": "2244994945"
            }
          ]
        }
      },
      "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": "1012"
        },
        {
          "key": "content-type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "date",
          "value": "Fri, 26 Jun 2020 15:33:41 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": "bf6b8cccb0bc95bc56a50be4db9a2d09"
        },
        {
          "key": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "key": "x-frame-options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "x-rate-limit-limit",
          "value": "15"
        },
        {
          "key": "x-rate-limit-remaining",
          "value": "1"
        },
        {
          "key": "x-rate-limit-reset",
          "value": "1593186139"
        },
        {
          "key": "x-response-time",
          "value": "76"
        },
        {
          "key": "x-xss-protection",
          "value": "0"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": {\n        \"created_at\": \"2013-12-14T04:35:55.000Z\",\n        \"description\": \"The voice of Twitter's #DevRel team, and your official source for updates, news, & events about Twitter's API. \\n\\n#BlackLivesMatter\",\n        \"entities\": {\n            \"url\": {\n                \"urls\": [\n                    {\n                        \"start\": 0,\n                        \"end\": 23,\n                        \"url\": \"https://t.co/3ZX3TNiZCY\",\n                        \"expanded_url\": \"https://developer.twitter.com/en/community\",\n                        \"display_url\": \"developer.twitter.com/en/community\"\n                    }\n                ]\n            },\n            \"description\": {\n                \"hashtags\": [\n                    {\n                        \"start\": 23,\n                        \"end\": 30,\n                        \"tag\": \"DevRel\"\n                    },\n                    {\n                        \"start\": 113,\n                        \"end\": 130,\n                        \"tag\": \"BlackLivesMatter\"\n                    }\n                ]\n            }\n        },\n        \"id\": \"2244994945\",\n        \"location\": \"127.0.0.1\",\n        \"name\": \"Twitter Dev\",\n        \"pinned_tweet_id\": \"1255542774432063488\",\n        \"profile_image_url\": \"https://pbs.twimg.com/profile_images/1267175364003901441/tBZNFAgA_normal.jpg\",\n        \"protected\": false,\n        \"url\": \"https://t.co/3ZX3TNiZCY\",\n        \"username\": \"TwitterDev\",\n        \"verified\": true\n    },\n    \"includes\": {\n        \"tweets\": [\n            {\n                \"author_id\": \"2244994945\",\n                \"conversation_id\": \"1255542774432063488\",\n                \"created_at\": \"2020-04-29T17:01:38.000Z\",\n                \"entities\": {\n                    \"urls\": [\n                        {\n                            \"start\": 264,\n                            \"end\": 287,\n                            \"url\": \"https://t.co/BPqMcQzhId\",\n                            \"expanded_url\": \"https://blog.twitter.com/developer/en_us/topics/tools/2020/covid19_public_conversation_data.html\",\n                            \"display_url\": \"blog.twitter.com/developer/en_u…\",\n                            \"images\": [\n                                {\n                                    \"url\": \"https://pbs.twimg.com/news_img/1274730688919613440/SEsqlE16?format=jpg&name=orig\",\n                                    \"width\": 1600,\n                                    \"height\": 600\n                                },\n                                {\n                                    \"url\": \"https://pbs.twimg.com/news_img/1274730688919613440/SEsqlE16?format=jpg&name=150x150\",\n                                    \"width\": 150,\n                                    \"height\": 150\n                                }\n                            ],\n                            \"status\": 200,\n                            \"title\": \"Enabling study of the public conversation in a time of crisis\",\n                            \"description\": \"To further support our company’s ongoing efforts, we want to enable developers and researchers to study the public conversation on COVID-19 in real-time.\",\n                            \"unwound_url\": \"https://blog.twitter.com/developer/en_us/topics/tools/2020/covid19_public_conversation_data.html\"\n                        }\n                    ]\n                },\n                \"id\": \"1255542774432063488\",\n                \"lang\": \"en\",\n                \"possibly_sensitive\": false,\n                \"source\": \"Twitter Web App\",\n                \"text\": \"During these unprecedented times, what’s happening on Twitter can help the world better understand &amp; respond to the pandemic. \\n\\nWe're launching a free COVID-19 stream endpoint so qualified devs &amp; researchers can study the public conversation in real-time. https://t.co/BPqMcQzhId\"\n            }\n        ]\n    }\n}"
    }
  ]
}