-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_facebook_access_token
executable file
·60 lines (48 loc) · 1.69 KB
/
get_facebook_access_token
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
#!/usr/bin/env python
import argparse
import requests
GRAPH_URL = "https://graph.facebook.com/v13.0"
parser = argparse.ArgumentParser()
parser.add_argument("--app-id", required=True)
parser.add_argument("--app-secret", required=True)
parser.add_argument("--page-id", required=True)
parser.add_argument("--access-token", required=True)
parser.add_argument("--test-post", action="store_true")
args = parser.parse_args()
short_lived_user_access_token = args.access_token
APP_ID = args.app_id
APP_SECRET = args.app_secret
PAGE_ID = args.page_id
print("[-] Retrieving long lived access token...")
r = requests.get(
f"{GRAPH_URL}/oauth/access_token",
params={
"grant_type": "fb_exchange_token",
"client_id": APP_ID,
"client_secret": APP_SECRET,
"fb_exchange_token": short_lived_user_access_token,
},
)
r.raise_for_status()
long_lived_user_access_token = r.json()["access_token"]
print(f"[+] Success: {long_lived_user_access_token}")
print("[-] Retrieving page acess token...")
r = requests.get(
f"{GRAPH_URL}/{PAGE_ID}",
params={"fields": "access_token", "access_token": long_lived_user_access_token},
)
r.raise_for_status()
page_access_token = r.json()["access_token"]
print(f"[+] Success: {page_access_token}")
if args.test_post:
print("[-] Testing page access token by posting test post...")
s = requests.Session()
s.params["access_token"] = page_access_token
r = s.post(f"{GRAPH_URL}/{PAGE_ID}/feed", data={"message": "test"})
r.raise_for_status()
post_id = r.json()["id"]
print(f"[+] Success, id: {post_id}")
print("[-] Deleting test post...")
r = s.delete(f"{GRAPH_URL}/{post_id}")
r.raise_for_status()
print("[+] Success.")