-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcreate.py
More file actions
54 lines (39 loc) · 1.49 KB
/
create.py
File metadata and controls
54 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Create Bookmark - X API v2
==========================
Endpoint: POST https://api.x.com/2/users/:id/bookmarks
Docs: https://developer.x.com/en/docs/twitter-api/tweets/bookmarks/api-reference/post-users-id-bookmarks
Authentication: OAuth 2.0 with PKCE (User Context)
Required env vars: BEARER_TOKEN (OAuth 2.0 user access token)
"""
import requests
import os
import json
# Note: This endpoint requires OAuth 2.0 User Context
# The bearer_token here should be the user's access token from OAuth 2.0 PKCE flow
bearer_token = os.environ.get("BEARER_TOKEN")
def create_url(user_id):
return "https://api.x.com/2/users/{}/bookmarks".format(user_id)
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "v2CreateBookmarkPython"
return r
def connect_to_endpoint(url, payload):
response = requests.post(url, auth=bearer_oauth, json=payload)
print(response.status_code)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()
def main():
# Replace with the authenticated user's ID
user_id = "your-user-id"
# Replace with the post ID you want to bookmark
payload = {"tweet_id": "post-id-to-bookmark"}
url = create_url(user_id)
json_response = connect_to_endpoint(url, payload)
print(json.dumps(json_response, indent=4, sort_keys=True))
if __name__ == "__main__":
main()