Objective-C / Twitter API v2 / User by Username
Back to Collection Items
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;
// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = @"<access_token>";
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [http QuickGetSb: @"https://api.twitter.com/2/users/by/username/:username" sbContent: sbResponseBody];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);
int respStatusCode = [http.LastStatus intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",http.LastHeader);
NSLog(@"%@",@"Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "errors": [
// {
// "detail": "Could not find user with username: [TwitterDec].",
// "title": "Not Found Error",
// "resource_type": "user",
// "parameter": "username",
// "value": "TwitterDec",
// "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
NSString *detail = 0;
NSString *title = 0;
NSString *resource_type = 0;
NSString *parameter = 0;
NSString *value = 0;
NSString *v_type = 0;
int i = 0;
int count_i = [[jResp SizeOfArray: @"errors"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: 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;
}
Curl Command
curl -X GET
-H "Authorization: Bearer <access_token>"
https://api.twitter.com/2/users/by/username/:username
Postman Collection Item JSON
{
"name": "User by Username",
"request": {
"method": "GET",
"header": [
],
"url": {
"raw": "https://api.twitter.com/2/users/by/username/:username",
"protocol": "https",
"host": [
"api",
"twitter",
"com"
],
"path": [
"2",
"users",
"by",
"username",
":username"
],
"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,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": "username",
"value": "",
"type": "string",
"description": "Required. Enter a single Twitter username (handle)."
}
]
},
"description": "This endpoint returns details about a user by username.\n\nFor full details, see the [API reference](https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username) 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/by/username/:username",
"protocol": "https",
"host": [
"api",
"twitter",
"com"
],
"path": [
"2",
"users",
"by",
"username",
":username"
],
"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": "username",
"value": "TwitterDec"
}
]
}
},
"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": "184"
},
{
"key": "content-type",
"value": "application/json; charset=utf-8"
},
{
"key": "date",
"value": "Fri, 26 Jun 2020 15:32:08 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": "4"
},
{
"key": "x-rate-limit-reset",
"value": "1593186139"
},
{
"key": "x-response-time",
"value": "207"
},
{
"key": "x-xss-protection",
"value": "0"
}
],
"cookie": [
],
"body": "{\n \"errors\": [\n {\n \"detail\": \"Could not find user with username: [TwitterDec].\",\n \"title\": \"Not Found Error\",\n \"resource_type\": \"user\",\n \"parameter\": \"username\",\n \"value\": \"TwitterDec\",\n \"type\": \"https://api.twitter.com/2/problems/resource-not-found\"\n }\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/by/username/:username?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",
"by",
"username",
":username"
],
"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": "username",
"value": "TwitterDev"
}
]
}
},
"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:29:38 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": "8"
},
{
"key": "x-rate-limit-reset",
"value": "1593186139"
},
{
"key": "x-response-time",
"value": "253"
},
{
"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 & respond to the pandemic. \\n\\nWe're launching a free COVID-19 stream endpoint so qualified devs & researchers can study the public conversation in real-time. https://t.co/BPqMcQzhId\"\n }\n ]\n }\n}"
},
{
"name": "429 Rate Limit Exceeded",
"originalRequest": {
"method": "GET",
"header": [
],
"url": {
"raw": "https://api.twitter.com/2/users/by/username/:username?user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,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",
"by",
"username",
":username"
],
"query": [
{
"key": "user.fields",
"value": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,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": "username",
"value": "TwitterDev"
}
]
}
},
"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:18:23 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": "1593185073"
},
{
"key": "x-response-time",
"value": "7"
}
],
"cookie": [
],
"body": "Rate limit exceeded\n"
},
{
"name": "200 Success - Default Payload",
"originalRequest": {
"method": "GET",
"header": [
],
"url": {
"raw": "https://api.twitter.com/2/users/by/username/:username",
"protocol": "https",
"host": [
"api",
"twitter",
"com"
],
"path": [
"2",
"users",
"by",
"username",
":username"
],
"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": "username",
"value": "TwitterDev"
}
]
}
},
"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 15:31:35 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": "6"
},
{
"key": "x-rate-limit-reset",
"value": "1593186139"
},
{
"key": "x-response-time",
"value": "37"
},
{
"key": "x-xss-protection",
"value": "0"
}
],
"cookie": [
],
"body": "{\n \"data\": {\n \"id\": \"2244994945\",\n \"name\": \"Twitter Dev\",\n \"username\": \"TwitterDev\"\n }\n}"
}
]
}