Chilkat Online Tools

Ruby / Twitter API v2 / User by ID

Back to Collection Items

require 'chilkat'

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

http = Chilkat::CkHttp.new()

# Adds the "Authorization: Bearer <access_token>" header.
http.put_AuthToken("<access_token>")

sbResponseBody = Chilkat::CkStringBuilder.new()
success = http.QuickGetSb("https://api.twitter.com/2/users/:id",sbResponseBody)
if (success == false)
    print http.lastErrorText() + "\n";
    exit
end

jResp = Chilkat::CkJsonObject.new()
jResp.LoadSb(sbResponseBody)
jResp.put_EmitCompact(false)

print "Response Body:" + "\n";
print jResp.emit() + "\n";

respStatusCode = http.get_LastStatus()
print "Response Status Code = " + respStatusCode.to_s() + "\n";
if (respStatusCode >= 400)
    print "Response Header:" + "\n";
    print http.lastHeader() + "\n";
    print "Failed." + "\n";
    exit
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

i = 0
count_i = jResp.SizeOfArray("errors")
while i < count_i
    jResp.put_I(i)
    detail = jResp.stringOf("errors[i].detail")
    title = jResp.stringOf("errors[i].title")
    resource_type = jResp.stringOf("errors[i].resource_type")
    parameter = jResp.stringOf("errors[i].parameter")
    value = jResp.stringOf("errors[i].value")
    v_type = jResp.stringOf("errors[i].type")
    i = i + 1
end

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}"
    }
  ]
}