-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_linkedin_publish.py
More file actions
73 lines (57 loc) · 2.38 KB
/
Copy path10_linkedin_publish.py
File metadata and controls
73 lines (57 loc) · 2.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""LinkedIn publish — round-trip post → read → react → delete.
This example demonstrates the LinkedIn connector against a real LinkedIn
account. It posts a clearly-labelled test update to your network feed
(visibility=CONNECTIONS, not PUBLIC), reads it back, adds a LIKE, and
deletes it.
Prerequisites:
pip install "toolsconnector[linkedin]"
export TC_LINKEDIN_CREDENTIALS='your-oauth-access-token'
How to get the token:
ToolsConnector is BYOK (bring-your-own-key). Get a LinkedIn OAuth 2.0
access token from your own application's auth flow, your secrets
manager, or — for one-off testing — any OAuth client tool. The token
needs scopes: `openid profile email w_member_social`.
See https://www.linkedin.com/developers/apps for LinkedIn's
developer portal.
"""
import json
import os
from toolsconnector.serve import ToolKit
TOKEN = os.environ.get("TC_LINKEDIN_CREDENTIALS", "")
if not TOKEN:
raise SystemExit("set TC_LINKEDIN_CREDENTIALS env var first")
kit = ToolKit(connectors=["linkedin"], credentials={"linkedin": TOKEN})
def call(tool_name: str, args: dict) -> dict:
"""Call a tool and return its result as a dict.
``ToolKit.execute()`` returns the JSON-serialised string the MCP/HTTP
transport would emit on the wire. For Python-native callers we want
the parsed dict back, so we ``json.loads()`` it. Returns an empty
dict for tools whose result is None / empty (e.g. delete actions).
"""
raw = kit.execute(tool_name, args)
if not raw:
return {}
return json.loads(raw)
# 1. Identity — get the URN we need to post as.
profile = call("linkedin_get_profile", {})
author_urn = f"urn:li:person:{profile['sub']}"
print(f"Authenticated as: {profile['name']} ({author_urn})")
# 2. Publish a test post (CONNECTIONS visibility — only your network sees it).
post = call("linkedin_create_post", {
"author": author_urn,
"commentary": "Test post from ToolsConnector — please ignore.",
"visibility": "CONNECTIONS",
})
post_urn = post["id"]
print(f"Posted: {post_urn}")
print(f"View: https://www.linkedin.com/feed/update/{post_urn}/")
# 3. Add a LIKE reaction.
call("linkedin_react_to_post", {
"post_urn": post_urn,
"actor": author_urn,
"reaction_type": "LIKE",
})
print("Reacted: LIKE")
# 4. Clean up — delete the test post.
call("linkedin_delete_post", {"urn": post_urn})
print(f"Deleted: {post_urn}")