Chilkat Online Tools

Python / Twitter API v2 / Bookmark a Tweet

Back to Collection Items

import sys
import chilkat

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

http = chilkat.CkHttp()

# Use this online tool to generate code from sample JSON: Generate Code to Create JSON

# The following JSON is sent in the request body.

# {
#   "tweet_id": "1503863301276827651"
# }

json = chilkat.CkJsonObject()
json.UpdateString("tweet_id","1503863301276827651")

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

# resp is a CkHttpResponse
resp = http.PostJson3("https://api.twitter.com/2/users/:id/bookmarks","application/json",json)
if (http.get_LastMethodSuccess() == False):
    print(http.lastErrorText())
    sys.exit()

print(str(resp.get_StatusCode()))
print(resp.bodyStr())

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "tweet_id": "1503863301276827651"
}'
https://api.twitter.com/2/users/:id/bookmarks

Postman Collection Item JSON

{
  "name": "Bookmark a Tweet",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "// This script extracts the ID of the authenticating user, so you do not have to pass it yourself.",
          "if (!pm.environment.get('access_token') || !pm.request.url.path.includes(':id')) {",
          "    return;",
          "}",
          "",
          "const [userId] = pm.environment.get('access_token').split('-');",
          "",
          "if (!userId) {",
          "    return;",
          "}",
          "",
          "const userIdIndex = pm.request.url.path.indexOf(':id');",
          "pm.request.url.path[userIdIndex] = userId;"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "oauth2",
      "oauth2": [
        {
          "key": "state",
          "value": "",
          "type": "string"
        },
        {
          "key": "scope",
          "value": "",
          "type": "string"
        },
        {
          "key": "accessTokenUrl",
          "value": "",
          "type": "string"
        },
        {
          "key": "authUrl",
          "value": "",
          "type": "string"
        },
        {
          "key": "tokenName",
          "value": "bookmarks",
          "type": "string"
        },
        {
          "key": "redirect_uri",
          "value": "",
          "type": "string"
        },
        {
          "key": "clientId",
          "value": "",
          "type": "string"
        },
        {
          "key": "grant_type",
          "value": "authorization_code_with_pkce",
          "type": "string"
        },
        {
          "key": "addTokenTo",
          "value": "header",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"tweet_id\": \"1503863301276827651\"\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.twitter.com/2/users/:id/bookmarks",
      "protocol": "https",
      "host": [
        "api",
        "twitter",
        "com"
      ],
      "path": [
        "2",
        "users",
        ":id",
        "bookmarks"
      ],
      "variable": [
        {
          "key": "id",
          "value": "",
          "description": "The ID of the user who wishes to like a Tweet. The ID must belong to the authenticating user.\n"
        }
      ]
    },
    "description": "Allows an authenticated user to like a Tweet.\n\nFor full details, see the [API reference](https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/post-users-user_id-bookmarks) for this endpoint.\n\n[Sign up](https://t.co/signup) for the Twitter API"
  },
  "response": [
  ]
}