forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_bookmark.py
109 lines (87 loc) · 3.76 KB
/
delete_bookmark.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import base64
import hashlib
import os
import re
import json
import requests
from requests.auth import AuthBase, HTTPBasicAuth
from requests_oauthlib import OAuth2Session
# Replace with a Tweet ID you want to remove Bookmark of
bookmarked_tweet_id = "1460323737035677698"
# You will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_ID='your-client-id'
client_id = os.environ.get("CLIENT_ID")
# If you have selected a type of App that is a confidential client you will need to set a client secret.
# Confidential Clients securely authenticate with the authorization server.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_SECRET='your-client-secret'
# Remove the comment on the following line if you are using a confidential client
# client_secret = os.environ.get("CLIENT_SECRET")
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
redirect_uri = "https://www.example.com"
# Set the scopes
scopes = ["bookmark.write", "tweet.read", "users.read", "offline.access"]
# Create a code verifier
code_verifier = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8")
code_verifier = re.sub("[^a-zA-Z0-9]+", "", code_verifier)
# Create a code challenge
code_challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
code_challenge = base64.urlsafe_b64encode(code_challenge).decode("utf-8")
code_challenge = code_challenge.replace("=", "")
# Start and OAuth 2.0 session
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scopes)
# Create an authorize URL
auth_url = "https://twitter.com/i/oauth2/authorize"
authorization_url, state = oauth.authorization_url(
auth_url, code_challenge=code_challenge, code_challenge_method="S256"
)
# Visit the URL to authorize your App to make requests on behalf of a user
print(
"Visit the following URL to authorize your App on behalf of your Twitter handle in a browser:"
)
print(authorization_url)
# Paste in your authorize URL to complete the request
authorization_response = input(
"Paste in the full URL after you've authorized your App:\n"
)
# Fetch your access token
token_url = "https://api.twitter.com/2/oauth2/token"
# The following line of code will only work if you are using a type of App that is a public client
auth = False
# If you are using a confidential client you will need to pass in basic encoding of your client ID and client secret.
# Please remove the comment on the following line if you are using a type of App that is a confidential client
# auth = HTTPBasicAuth(client_id, client_secret)
token = oauth.fetch_token(
token_url=token_url,
authorization_response=authorization_response,
auth=auth,
client_id=client_id,
include_client_id=True,
code_verifier=code_verifier,
)
# Your access token
access = token["access_token"]
# Make a request to the users/me endpoint to get the user ID of the authenticated user
user_me = requests.request(
"GET",
"https://api.twitter.com/2/users/me",
headers={"Authorization": "Bearer {}".format(access)},
).json()
user_id = user_me["data"]["id"]
# Make a request to the bookmarks url
url = "https://api.twitter.com/2/users/{}/bookmarks/{}".format(
user_id, bookmarked_tweet_id
)
headers = {
"Authorization": "Bearer {}".format(access),
"User-Agent": "BookmarksSampleCode",
}
response = requests.request("DELETE", url, headers=headers)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(response.status_code, response.text)
)
print("Response code: {}".format(response.status_code))
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))