Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kinlane/4445917dcdd28a1be958545f41e2caa3 to your computer and use it in GitHub Desktop.
Save kinlane/4445917dcdd28a1be958545f41e2caa3 to your computer and use it in GitHub Desktop.
parse-twitter-bookmarks-api-as-postman-collection-test.js
var jsonData = pm.response.json();
var tweets = '';
var all_links = [];
// list out all our links.
tweets = "<h2>Bookmarks</h2>";
tweets = tweets + '<ul>';
var tweet_results = jsonData.globalObjects.tweets;
// loop through all the favorited tweets from this respons
for (const [key, value] of Object.entries(tweet_results)) {
var text = value.full_text;
var url = value.entities.urls;
var id = key;
tweets = tweets + '<li>';
tweets = tweets + text + ' (tweet id:' + id + ')';
if(url.length > 0){
tweets = tweets + text + '<ul>';
for (var i = 0; i < url.length; i++){
var expanded_url = url[i].expanded_url;
tweets = tweets + '<li>' + expanded_url + '</li>';
}
tweets = tweets + '</ul>';
}
tweets = tweets + '</li>';
}
tweets = tweets + '</ul>';
pm.visualizer.set(tweets);
pm.environment.set("favorites",tweets);
@Mera-balou
Copy link

Hi Kin,

Thank you for your great writeup on how to "Pull Twitter Bookmarks Via The Twitter API".

I managed to get the twitter response JSON with my bookmarks. But when i paste your script on the "Tests tab" and click send again, i received this error on the "Test Results" tab:

There was an error in evaluating the test script: TypeError: Cannot read property 'length' of undefined

I tried to understand your javascript code to spot the error but it is above my skills for now. Do you have any idea what I'm doing wrong please?

Regards

@postkin
Copy link

postkin commented Feb 9, 2020

The error most likely reflects you aren't getting any response from Twitter -- do you see JSON return as part of the API request? It not, the script won't find any tweets.

@Mera-balou
Copy link

I well received a JSON return from Twitter.
You will find below the JSON that I receive (I changed the parameter count to 2 for this example in order to receive my 2 last bookmarks). I always receive the same error mentioned in my first message in the "Test Results' tab.

{
"globalObjects": {
"tweets": {
"1226124123174264832": {
"created_at": "Sat Feb 08 12:42:25 +0000 2020",
"id_str": "1226124123174264832",
"full_text": "Agile is 1. reducing the distance between problems and problem solvers; 2. breaking down big problems into smaller steps; 3. validating each step; 4. improving as you go. Everything else is either selling something or trying to make Agile a synonym for “good”.",
"display_text_range": [
0,
260
],
"entities": {},
"source": "<a href="http://twitter.com/#!/download/ipad\" rel="nofollow">Twitter for iPad",
"user_id_str": "17437733",
"retweet_count": 146,
"favorite_count": 402,
"reply_count": 8,
"conversation_id_str": "1226124123174264832",
"lang": "en"
},
"1226376719214399489": {
"created_at": "Sun Feb 09 05:26:09 +0000 2020",
"id_str": "1226376719214399489",
"full_text": "Hells to the yes! @deviantollam and @babakjavadi talking to the @modernrogueshow about how to clone RFID cards and attacking Badge Readers?! So good!\n\nThat RFID teaching aide is awesome btw. Are the instructions to make that somewhere online?\n\nhttps://t.co/T9ypo1oBGC",
"display_text_range": [
0,
267
],
"entities": {
"user_mentions": [
{
"screen_name": "deviantollam",
"name": "Deviant Ollam ツ",
"id_str": "123405224",
"indices": [
18,
31
]
},
{
"screen_name": "babakjavadi",
"name": "Babak Javadi",
"id_str": "135775505",
"indices": [
36,
48
]
},
{
"screen_name": "modernrogueshow",
"name": "Modern Rogue",
"id_str": "949107465005813761",
"indices": [
64,
80
]
}
],
"urls": [
{
"url": "https://t.co/T9ypo1oBGC",
"expanded_url": "https://www.youtube.com/watch?v=Ccm1caB6bao",
"display_url": "youtube.com/watch?v=Ccm1ca…",
"indices": [
244,
267
]
}
]
},
"source": "<a href="https://mobile.twitter.com\" rel="nofollow">Twitter Web App",
"user_id_str": "3686191",
"retweet_count": 3,
"favorite_count": 11,
"reply_count": 0,
"conversation_id_str": "1226376719214399489",
"possibly_sensitive_editable": true,
"card": {
"name": "player",
"url": "https://t.co/T9ypo1oBGC",
"card_type_url": "http://card-type-url-is-deprecated.invalid",
"binding_values": {
"vanity_url": {
"type": "STRING",
"string_value": "youtube.com",
"scribe_key": "vanity_url"
},
"player_url": {
"type": "STRING",
"string_value": "https://www.youtube.com/embed/Ccm1caB6bao"
},
"app_is_free": {
"type": "STRING",
"string_value": "true"
},
"app_price_currency": {
"type": "STRING",
"string_value": "USD"
},
"app_price_amount": {
"type": "STRING",
"string_value": "0.0"
},
"domain": {
"type": "STRING",
"string_value": "www.youtube.com"
},
"app_num_ratings": {
"type": "STRING",
"string_value": "52,009"
},
"app_star_rating": {
"type": "STRING",
"string_value": "4.5"
},
"app_name": {
"type": "STRING",
"string_value": "YouTube: Watch, Listen, Stream"
},
"player_width": {
"type": "STRING",
"string_value": "1280"
},
"player_height": {
"type": "STRING",
"string_value": "720"
},
"site": {
"type": "USER",
"user_value": {
"id_str": "10228272",
"path": []
},
"scribe_key": "publisher_id"
},
"title": {
"type": "STRING",
"string_value": "How to Bypass RFID Badge Readers (w/ Deviant Ollam and Babak Javadi)"
},
"description": {
"type": "STRING",
"string_value": "Go to http://privacy.com/rogue to get $5 free money! 🡇 --------------- extra material & accreditation --------------- 🡇 If you can see this video, we have su..."
},
"player_image_small": {
"type": "IMAGE",
"image_value": {
"url": "https://pbs.twimg.com/card_img/1225181156729085956/AHNp4ITl?format=jpg&name=144x144",
"width": 144,
"height": 81
}
},
"player_image": {
"type": "IMAGE",
"image_value": {
"url": "https://pbs.twimg.com/card_img/1225181156729085956/AHNp4ITl?format=jpg&name=280x280",
"width": 280,
"height": 158
}
},
"player_image_large": {
"type": "IMAGE",
"image_value": {
"url": "https://pbs.twimg.com/card_img/1225181156729085956/AHNp4ITl?format=jpg&name=800x320_1",
"width": 569,
"height": 320
}
},
"player_image_x_large": {
"type": "IMAGE",
"image_value": {
"url": "https://pbs.twimg.com/card_img/1225181156729085956/AHNp4ITl?format=png&name=2048x2048_2_exp",
"width": 1280,
"height": 720
}
},
"player_image_color": {
"type": "IMAGE_COLOR",
"image_color_value": {
"palette": [
{
"percentage": 22.61,
"rgb": {
"red": 30,
"green": 20,
"blue": 19
}
},
{
"percentage": 21.19,
"rgb": {
"red": 190,
"green": 247,
"blue": 214
}
},
{
"percentage": 17.49,
"rgb": {
"red": 254,
"green": 162,
"blue": 154
}
},
{
"percentage": 7.16,
"rgb": {
"red": 115,
"green": 67,
"blue": 45
}
},
{
"percentage": 6.3,
"rgb": {
"red": 254,
"green": 127,
"blue": 119
}
}
]
}
},
"player_image_original": {
"type": "IMAGE",
"image_value": {
"url": "https://pbs.twimg.com/card_img/1225181156729085956/AHNp4ITl?format=jpg&name=orig",
"width": 1280,
"height": 720
}
},
"card_url": {
"type": "STRING",
"string_value": "https://t.co/T9ypo1oBGC",
"scribe_key": "card_url"
}
},
"users": {
"10228272": {
"id_str": "10228272",
"name": "YouTube",
"screen_name": "YouTube",
"location": "San Bruno, CA",
"description": "Like and subscribe.",
"url": "https://t.co/qkVaJFk2CG",
"entities": {
"url": {
"urls": [
{
"url": "https://t.co/qkVaJFk2CG",
"expanded_url": "https://www.youtube.com/",
"display_url": "youtube.com",
"indices": [
0,
23
]
}
]
},
"description": {}
},
"followers_count": 72297447,
"fast_followers_count": 390,
"normal_followers_count": 72297057,
"friends_count": 1052,
"listed_count": 81815,
"created_at": "Tue Nov 13 21:43:46 +0000 2007",
"favourites_count": 2816,
"verified": true,
"statuses_count": 24618,
"media_count": 9545,
"is_translation_enabled": true,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1209179754273730567/ocZKRu9P_normal.jpg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/10228272/1579878841",
"profile_image_extensions_alt_text": null,
"profile_image_extensions_media_availability": null,
"profile_image_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 255,
"green": 255,
"blue": 255
},
"percentage": 70.92
},
{
"rgb": {
"red": 254,
"green": 0,
"blue": 0
},
"percentage": 27.54
},
{
"rgb": {
"red": 233,
"green": 171,
"blue": 174
},
"percentage": 0.91
},
{
"rgb": {
"red": 229,
"green": 76,
"blue": 84
},
"percentage": 0.46
},
{
"rgb": {
"red": 235,
"green": 129,
"blue": 128
},
"percentage": 0.13
}
]
},
"profile_image_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_banner_extensions_alt_text": null,
"profile_banner_extensions_media_availability": null,
"profile_banner_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 211,
"green": 207,
"blue": 223
},
"percentage": 33.38
},
{
"rgb": {
"red": 152,
"green": 179,
"blue": 231
},
"percentage": 8.9
},
{
"rgb": {
"red": 208,
"green": 182,
"blue": 141
},
"percentage": 5.47
},
{
"rgb": {
"red": 221,
"green": 166,
"blue": 186
},
"percentage": 4.34
},
{
"rgb": {
"red": 163,
"green": 201,
"blue": 188
},
"percentage": 3.45
}
]
},
"profile_banner_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_link_color": "C9191D",
"pinned_tweet_ids": [],
"pinned_tweet_ids_str": [],
"has_custom_timelines": true,
"advertiser_account_type": "promotable_user",
"advertiser_account_service_levels": [
"dso",
"dso",
"dso",
"dso",
"dso"
],
"profile_interstitial_type": "",
"business_profile_state": "none",
"translator_type": "regular"
}
},
"card_platform": {
"platform": {
"device": {
"name": "Swift",
"version": "12"
},
"audience": {
"name": "production"
}
}
}
},
"lang": "en"
}
},
"users": {
"17437733": {
"id_str": "17437733",
"name": "Jason Yip",
"screen_name": "jchyip",
"location": "New York, NY",
"description": "Senior Agile Coach at Spotify NYC, ex-ThoughtWorks, ex-CruiseControl",
"url": "https://t.co/wiZ7MPCthV",
"entities": {
"url": {
"urls": [
{
"url": "https://t.co/wiZ7MPCthV",
"expanded_url": "https://medium.com/@jchyip",
"display_url": "medium.com/@jchyip",
"indices": [
0,
23
]
}
]
},
"description": {}
},
"followers_count": 5718,
"fast_followers_count": 0,
"normal_followers_count": 5718,
"friends_count": 3197,
"listed_count": 469,
"created_at": "Mon Nov 17 08:55:08 +0000 2008",
"favourites_count": 885,
"geo_enabled": true,
"statuses_count": 88975,
"media_count": 922,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/826165345677410305/F4Nc3dtB_normal.jpg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/17437733/1398575836",
"profile_image_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 225,
"green": 253,
"blue": 255
},
"percentage": 51.58
},
{
"rgb": {
"red": 139,
"green": 124,
"blue": 112
},
"percentage": 17.47
},
{
"rgb": {
"red": 24,
"green": 31,
"blue": 49
},
"percentage": 15.08
},
{
"rgb": {
"red": 81,
"green": 139,
"blue": 140
},
"percentage": 9.47
},
{
"rgb": {
"red": 85,
"green": 73,
"blue": 68
},
"percentage": 2.47
}
]
},
"profile_image_extensions_alt_text": null,
"profile_image_extensions_media_availability": null,
"profile_image_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_banner_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 139,
"green": 177,
"blue": 205
},
"percentage": 54.61
},
{
"rgb": {
"red": 86,
"green": 138,
"blue": 192
},
"percentage": 21.69
},
{
"rgb": {
"red": 91,
"green": 98,
"blue": 55
},
"percentage": 12.75
},
{
"rgb": {
"red": 192,
"green": 212,
"blue": 219
},
"percentage": 8.58
},
{
"rgb": {
"red": 103,
"green": 121,
"blue": 110
},
"percentage": 1.8
}
]
},
"profile_banner_extensions_alt_text": null,
"profile_banner_extensions_media_availability": null,
"profile_banner_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_link_color": "1B95E0",
"has_extended_profile": true,
"pinned_tweet_ids": [
1213867726768427008
],
"pinned_tweet_ids_str": [
"1213867726768427008"
],
"has_custom_timelines": true,
"advertiser_account_type": "promotable_user",
"advertiser_account_service_levels": [],
"profile_interstitial_type": "",
"business_profile_state": "none",
"translator_type": "none"
},
"3686191": {
"id_str": "3686191",
"name": "Louis Cremen 👨🏻‍💻",
"screen_name": "proxyblue",
"location": "Wollongong, Australia",
"description": "InfoSec Guy, Software Engineer & Trainer. ❤️Neurotechnology, Security, Automation, TheManyHatsClub,  & the future. Currently reading the internet.",
"url": "https://t.co/4gOkyC99X0",
"entities": {
"url": {
"urls": [
{
"url": "https://t.co/4gOkyC99X0",
"expanded_url": "https://louis.land",
"display_url": "louis.land",
"indices": [
0,
23
]
}
]
},
"description": {}
},
"followers_count": 1588,
"fast_followers_count": 0,
"normal_followers_count": 1588,
"friends_count": 1543,
"listed_count": 67,
"created_at": "Sat Apr 07 05:40:55 +0000 2007",
"favourites_count": 5052,
"geo_enabled": true,
"statuses_count": 6373,
"media_count": 665,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1067277572210274309/Ox9dexhP_normal.jpg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/3686191/1441861914",
"profile_image_extensions_alt_text": null,
"profile_image_extensions_media_availability": null,
"profile_image_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 255,
"green": 255,
"blue": 255
},
"percentage": 50.84
},
{
"rgb": {
"red": 22,
"green": 16,
"blue": 16
},
"percentage": 28.41
},
{
"rgb": {
"red": 159,
"green": 110,
"blue": 93
},
"percentage": 14.51
},
{
"rgb": {
"red": 218,
"green": 150,
"blue": 139
},
"percentage": 2.4
},
{
"rgb": {
"red": 216,
"green": 159,
"blue": 139
},
"percentage": 2.2
}
]
},
"profile_image_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_banner_extensions_media_color": {
"palette": [
{
"rgb": {
"red": 4,
"green": 35,
"blue": 60
},
"percentage": 52.27
},
{
"rgb": {
"red": 192,
"green": 207,
"blue": 209
},
"percentage": 21.69
},
{
"rgb": {
"red": 87,
"green": 117,
"blue": 155
},
"percentage": 8.59
},
{
"rgb": {
"red": 41,
"green": 87,
"blue": 143
},
"percentage": 7.32
},
{
"rgb": {
"red": 8,
"green": 13,
"blue": 22
},
"percentage": 6.21
}
]
},
"profile_banner_extensions_alt_text": null,
"profile_banner_extensions_media_availability": null,
"profile_banner_extensions": {
"mediaStats": {
"r": {
"missing": null
},
"ttl": -1
}
},
"profile_link_color": "5F4B8B",
"has_extended_profile": true,
"pinned_tweet_ids": [
882689349892145152
],
"pinned_tweet_ids_str": [
"882689349892145152"
],
"has_custom_timelines": true,
"advertiser_account_type": "promotable_user",
"advertiser_account_service_levels": [
"smb"
],
"profile_interstitial_type": "",
"business_profile_state": "none",
"translator_type": "none"
}
},
"moments": {},
"cards": {},
"places": {},
"media": {},
"broadcasts": {}
},
"timeline": {
"id": "Custom-1077699388317798400",
"instructions": [
{
"addEntries": {
"entries": [
{
"entryId": "tweet-1226376719214399489",
"sortIndex": "414522671255567616",
"content": {
"item": {
"content": {
"tweet": {
"id": "1226376719214399489",
"displayType": "Tweet"
}
}
}
}
},
{
"entryId": "tweet-1226124123174264832",
"sortIndex": "414522584062025216",
"content": {
"item": {
"content": {
"tweet": {
"id": "1226124123174264832",
"displayType": "Tweet"
}
}
}
}
},
{
"entryId": "cursor-top-414522671255567617",
"sortIndex": "414522671255567617",
"content": {
"operation": {
"cursor": {
"value": "HCaA9MW4i4vXwAsAAA==",
"cursorType": "Top"
}
}
}
},
{
"entryId": "cursor-bottom-414522584062025215",
"sortIndex": "414522584062025215",
"content": {
"operation": {
"cursor": {
"value": "HBaA+KbmgYbXwAsAAA==",
"cursorType": "Bottom",
"stopOnEmptyResponse": true
}
}
}
}
]
}
}
],
"responseObjects": {
"feedbackActions": {}
}
}
}

@4chr4f
Copy link

4chr4f commented Jul 27, 2020

all works great just had to check url against undefined

var jsonData = pm.response.json();

var tweets = '';
var all_links = [];

// list out all our links.
tweets = "

Bookmarks

";
tweets = tweets + '
    ';

    var tweet_results = jsonData.globalObjects.tweets;

    // loop through all the favorited tweets from this respons
    for (const [key, value] of Object.entries(tweet_results)) {

    var text = value.full_text;
    var url = value.entities.urls;
    var id = key;
    
    tweets = tweets + '<li>';
    tweets = tweets + text  + ' (tweet id:' + id  + ')';
    
    if(undefined !== url && url.length > 0){
    
        tweets = tweets + text  + '<ul>';
        
        for (var i = 0; i < url.length; i++){
           var expanded_url = url[i].expanded_url;
           tweets = tweets + '<li>' + expanded_url  + '</li>';
        }
        
        tweets = tweets  + '</ul>';
    }
    
    tweets = tweets + '</li>';
    
    }
    

    tweets = tweets + '

';
pm.visualizer.set(tweets);
pm.environment.set("favorites",tweets);

@FireCode117
Copy link

FireCode117 commented Dec 31, 2020

Hello.

I get a different error when trying to run the script:

There was an error in evaluating the test script: JSONError: Unexpected token '<' at 1:1 ^

@postkin
Copy link

postkin commented Jan 4, 2021 via email

@cazvlad
Copy link

cazvlad commented Sep 24, 2021

@postkin

The GET API call you described in your article doesn't seem to exist anymore. When filtering for bookmark, these are the 3 results I get:
image

The first one pinged returns a crap mess of JS and no bookmarks.

The other two return 403 Forbidden.

Do you have any tips on navigating the situation?

Thank you so much for your support and documentation on this topic!

@kinlane
Copy link
Author

kinlane commented Sep 24, 2021

Sorry, my head hasn't been in here for a while -- don't have much to offer. Next time I pick up to play with I'll troubleshoot and provide some tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment