Spaces:
Sleeping
Sleeping
File size: 1,955 Bytes
fb07b17 | 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 | from agents import function_tool
import requests
from dotenv import find_dotenv, load_dotenv
import os
# # Constants
LINKEDIN_ACCESS_TOKEN = os.getenv("LINKEDIN_ACCESS_TOKEN")
load_dotenv(find_dotenv())
HEADERS = {
"Authorization": f"Bearer {LINKEDIN_ACCESS_TOKEN}",
"Content-Type": "application/json",
}
def get_user_id():
"""
Retrieve LinkedIn user profile information using the /userinfo endpoint.
Returns:
dict: User profile information or None if an error occurs.
"""
url = "https://api.linkedin.com/v2/userinfo"
try:
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching user info: {e}")
return None
# @traceable
@function_tool
def post_content_linkedin(content: str):
"""
Share content on LinkedIn.
Args:
params (RunContextWrapper[PostContentParams]): Parameters for the post.
Returns:
dict: API response or None if an error occurs.
"""
user_info = get_user_id()
if not user_info:
print("Failed to retrieve user information.")
return None
author_urn = f"urn:li:person:{user_info['sub']}"
url = "https://api.linkedin.com/v2/ugcPosts"
payload = {
"author": author_urn,
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {"text": content},
"shareMediaCategory": "NONE",
}
},
"visibility": {"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"},
}
try:
response = requests.post(url, headers=HEADERS, json=payload)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error posting content: {e}")
return None |