එක API එකකින් Python වලින් Instagram, TikTok සහ Facebook වලට post කරන්න
easyCoexistence සමඟ, Python කේතය එක එක network හි official API හරහා Instagram, Facebook, TikTok, YouTube සහ LinkedIn වලට post කරයි. සෑම ගිණුමක්ම hosted link එකක් හරහා එක් වරක් පුරනය වේ; ඉන්පසු ඔබේ script එක එක GET එකකින් නව TikTok, YouTube හෝ LinkedIn token එකක් ලබා ගනී, Instagram සහ Facebook calls easyCoexistence හි MCP tools හරහා යන අතර, ඒවා token එක තබා ගනී. MCP tools මගින් Python agent එකට එම ගිණුම් ලැබේ.
UTF-8 ලෙස ගණන් කරන විට, එක Instagram DM එකකින් API හරහා ගෙන යා හැකි වැඩිම පෙළ. emoji වලට වැඩි ඉඩ ගන්නා නිසා.
TikTok API for Business access token එක කොපමණ කාලයක් වලංගු වේද; එහි refresh token එක වසරක් වලංගු වේ.
YouTube API call එකක් සෑම එකකම රැගෙන යන Google user access token එක කොපමණ කාලයක් වලංගු වේද.
MCP Python SDK හි OAuth handler එක, එය client ලියාපදිංචි කරයි, PKCE සමඟ පුරනය වීම ධාවනය කරයි සහ tokens නැවුම් කරයි.
ඉදිරිපත් කළ comment, mention සහ DM events එක් වරක් පමණක් ලැබෙන අතර කිසිවිට ගබඩා නොවේ, එබැවින් එක එක event එක වහාම acknowledge කරන්න.
Python ට Instagram, Facebook, TikTok, YouTube හෝ LinkedIn සඳහා token එකක් ලැබෙන්නේ කෙසේද?
easyCoexistence හි Partner API හරහා connect session එකකින්, ඔබේ ec_live_ key එකෙන් සත්යාපනය කරමින්. channel අගය instagram, facebook, tiktok, youtube හෝ linkedin ලෙස සකසා POST /connect-sessions කළ විට, එක් පාරිභෝගිකයෙකු සඳහා පැයක් වලංගු hosted link එකක් ලැබේ. ඔවුන් network එකේම login සමඟ පුරනය වී, status=connected සහ channel_id සමඟ ඔබේ redirect_url වෙත යයි. ඔබේ backend එක query string එක විශ්වාස නොකර GET /connect-sessions/{session} සමඟ එය තහවුරු කළ යුතුය. Meta, TikTok, Google හෝ LinkedIn මත ඔබේ කේතයට තමන්ගේ developer යෙදුමක් අවශ්ය නොවේ.
ඉන්පසු සෑම කාර්යයක ආරම්භයේදීම credentials ඉල්ලන්න. GET /channels/{id}?credentials=1 මගින් network එකේ API base, එම ගිණුමේ එම network මත id එක සහ TikTok, YouTube සහ LinkedIn සඳහා easyCoexistence විසින් නැවුම්ව තබන access token එක ලැබේ. TikTok token එක පැය 24ක්, Google එක පැයක් වලංගු වේ, එබැවින් එය ගබඩා කිරීමට වඩා සෑම කාර්යයකටම වෙන වෙනම ලබාගන්න. Instagram සහ Facebook tokens කිසිවිටෙකත් ලබා නොදේ; ඒවායේ calls MCP tools හරහා යයි.
import os
import requests
EC = "https://easycoexistence.com/api/v1"
KEY = {"Authorization": f"Bearer {os.environ['EC_API_KEY']}"} # ec_live_...
# A hosted sign-in link for one customer on one network
session = requests.post(f"{EC}/connect-sessions", headers=KEY, json={
"channel": "instagram", # or facebook, tiktok, youtube, linkedin
"redirect_url": "https://app.example.com/connected",
"external_ref": "customer_4821",
}).json()
print(session["url"]) # they come back with ?session=...&status=connected&channel_id=...
def credentials(channel_id: str) -> dict:
"""The network's API base and a token that is valid now. Call it per job."""
r = requests.get(f"{EC}/channels/{channel_id}", headers=KEY, params={"credentials": 1})
r.raise_for_status()
creds = r.json()["credentials"]
if not creds.get("access_token"):
raise RuntimeError(creds.get("note", "This channel is not connected."))
return credsPython වලින් Instagram සහ Facebook වලට post කරන්නේ කෙසේද?
Instagram, Meta හි Graph API මත පියවර දෙකකින් publish කරයි, token එක bearer header එකක් ලෙස. image_url සහ caption සහිතව POST /{ig-id}/media කළ විට container එකක් සෑදේ; එහි status_code GET කර FINISHED ලෙස කියවුණු පසු, creation_id ලෙස container එක සහිතව POST /{ig-id}/media_publish කළ විට එය profile එකට යයි. Meta නිර්දේශ කරන්නේ මිනිත්තුවකට වරක් පමණ, මිනිත්තු පහක් දක්වා පරීක්ෂා කරන්නයි. Reel එකක් එම පියවරම අනුගමනය කරයි, image වෙනුවට media_type REELS සහ video_url සමඟ.
Facebook Page post එකක් එක call එකකි: message සහ, ඔබට අවශ්ය නම්, link එකක් සහිතව POST /{page-id}/feed. published false සහ දැන් සිට මිනිත්තු 10ක් සහ දින 30ක් අතර ඉදිරියට scheduled_publish_time එකක් එක් කළ විට, ඔබේ කිසිදු කාර්යයක් ක්රියාත්මක නොවී Meta එය එම මොහොතේ publish කරයි. දෙවිධ අවස්ථාවේම Meta මාධ්ය public URL එකකින් බාගනී, සහ Instagram JPEG රූප පමණක් පිළිගනී.
import time
import requests
def graph(c: dict, path: str, **body) -> dict:
"""Meta's Graph API: a POST when there is a body, a GET when there is none."""
r = requests.request("POST" if body else "GET", f"{c['graph_api_base']}/{path}",
json=body or None, headers={"Authorization": f"Bearer {c['access_token']}"})
r.raise_for_status()
return r.json()
def post_to_instagram(channel_id: str, image_url: str, caption: str) -> str:
c = credentials(channel_id)
ig = c["account_id_on_meta"]
container = graph(c, f"{ig}/media", image_url=image_url, caption=caption)["id"]
for _ in range(5): # Meta: check about once a minute, for up to five minutes
status = graph(c, f"{container}?fields=status_code")["status_code"]
if status == "FINISHED":
return graph(c, f"{ig}/media_publish", creation_id=container)["id"]
if status in ("ERROR", "EXPIRED"):
raise RuntimeError(f"Instagram could not process the media: {status}")
time.sleep(60)
raise TimeoutError("The container is still processing")
def schedule_on_facebook(channel_id: str, message: str, publish_at: int) -> str:
c = credentials(channel_id)
post = graph(c, f"{c['account_id_on_meta']}/feed", message=message,
published=False, scheduled_publish_time=publish_at) # unix seconds
return post["id"]Python වලින් YouTube වලට upload කර LinkedIn හි post කරන්නේ කෙසේද?
YouTube වීඩියෝව resumable upload එකක් ලෙස ගනී. පළමු request එක uploadType=resumable සහ part=snippet,status සමඟ upload endpoint එකට යන අතර title සහ status JSON ලෙස රැගෙන යයි; එහි පිළිතුරේ Location header එක session address එකයි. ඒ address එකට file එක PUT කළ විට 201 ලෙස පිළිතුරු දෙයි, නව වීඩියෝව සමඟ. privacyStatus private ලෙසත් publishAt වේලාවක් සමඟත් සකසූ විට, YouTube එම මොහොතේ වීඩියෝව තනිවම publish කරයි, සහ selfDeclaredMadeForKids මගින් එය දරුවන් සඳහා සාදා ඇත්දැයි දැක්වේ.
LinkedIn සඳහා bearer token, LinkedIn-Version header එක සහ X-Restli-Protocol-Version 2.0.0 සමඟ /rest/posts වෙත POST එකක් පමණි. body එක author නම් කර, text එක commentary ලෙස රැගෙන visibility, distribution සහ lifecycleState සකසයි. credentials මගින් author_urn ලබා දෙයි: company Page සඳහා urn:li:organization, profile සඳහා urn:li:person, සහ easyCoexistence භාවිතා කරන version එක; නව post එකේ URN එක x-restli-id header එකෙන් ආපසු ලැබේ.
import requests
def schedule_on_youtube(channel_id: str, path: str, title: str, publish_at: str) -> str:
c = credentials(channel_id)
auth = {"Authorization": f"Bearer {c['access_token']}"}
start = requests.post(
"https://www.googleapis.com/upload/youtube/v3/videos",
params={"uploadType": "resumable", "part": "snippet,status"},
headers={**auth, "X-Upload-Content-Type": "video/mp4"},
json={"snippet": {"title": title},
"status": {"privacyStatus": "private", "publishAt": publish_at, # ISO 8601
"selfDeclaredMadeForKids": False}},
)
start.raise_for_status()
with open(path, "rb") as video: # streamed, not read into memory
done = requests.put(start.headers["Location"], data=video,
headers={**auth, "Content-Type": "video/mp4"})
done.raise_for_status()
return done.json()["id"]
def post_on_linkedin(channel_id: str, text: str) -> str:
c = credentials(channel_id)
r = requests.post(f"{c['api_base']}/posts", headers={
"Authorization": f"Bearer {c['access_token']}",
"LinkedIn-Version": c["required_headers"]["LinkedIn-Version"], # YYYYMM
"X-Restli-Protocol-Version": "2.0.0",
}, json={
"author": c["author_urn"], # urn:li:organization:... or urn:li:person:...
"commentary": text,
"visibility": "PUBLIC",
"distribution": {"feedDistribution": "MAIN_FEED", "targetEntities": [],
"thirdPartyDistributionChannels": []},
"lifecycleState": "PUBLISHED",
"isReshareDisabledByAuthor": False,
})
r.raise_for_status()
return r.headers["x-restli-id"] # the new post's URNPython වලින් comment සහ DM කියවා reply කරන්නේ කෙසේද?
channel එක සඳහා forwarding ක්රියාත්මක කර, සෑම delivery එකක්ම සත්යාපනය කර, පසුව network එකේ API හරහා reply කරන්න. url සමඟ PUT /channels/{id}/webhook එකෙන් signing_secret එකක් ලැබෙන අතර, එය එක් වරක් පමණක් පෙන්වයි. Instagram සහ Facebook හි comments, mentions සහ DM, සහ TikTok හි comments, platform, account_id, external_id සහ entry ලෙස POST එකක් ලෙස පැමිණේ, entry එක network එක යැවූ ආකාරයටම. X-EC-Signature header එක t=<unix>,v1=<hex> ලෙස කියවෙන අතර, v1 යනු timestamp එකේ, dot එකේ සහ raw body එකේ HMAC-SHA256 එකයි. සෑම event එකක්ම එක් වරක් යවන අතර, පිටපතක් ගබඩා නොවී සහ retry නොවී, එබැවින් පළමුව acknowledge කර queue එකකින් වැඩ කරන්න.
Instagram DM එකකට reply එක /{ig-id}/messages වෙත යන්නේ පුද්ගලයාගේ අවසන් පණිවිඩයෙන් පැය 24ක් ඇතුළත, බයිට් 1,000ක් දක්වා. comment එකකට /{comment-id}/replies වෙත public reply එකක්, හෝ දින 7ක් ඇතුළත එක private reply එකක් ලැබේ. TikTok comments වලට public reply ලැබෙන්නේ business/comment/reply/create/ හරහාය, මන්ද TikTok හි DM නැත; YouTube සහ LinkedIn comments ඔවුන්ගේ API හරහා කාලසටහනකට අනුව කියවයි.
import hashlib, hmac, json, queue, time
import requests
from flask import Flask, abort, request
app = Flask(__name__)
jobs: queue.Queue = queue.Queue() # a worker calls reply_on_instagram() for each event
SECRETS: dict[str, str] = {} # channel_id -> signing_secret; keep them in your database
def forward_events(channel_id: str, url: str) -> None:
r = requests.put(f"{EC}/channels/{channel_id}/webhook", headers=KEY, json={"url": url})
r.raise_for_status()
SECRETS[channel_id] = r.json()["signing_secret"] # returned only this once
def verified(raw: bytes, header: str, secret: str) -> bool:
parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p)
t, v1 = parts.get("t", ""), parts.get("v1", "")
if not t.isdigit() or abs(time.time() - int(t)) > 300:
return False
mac = hmac.new(secret.encode(), t.encode() + b"." + raw, hashlib.sha256).hexdigest()
return hmac.compare_digest(mac.encode(), v1.encode())
@app.post("/hooks/social")
def social_event():
raw = request.get_data()
event = json.loads(raw) # platform, account_id, external_id, entry
secret = SECRETS.get(event.get("account_id"))
if not secret or not verified(raw, request.headers.get("X-EC-Signature", ""), secret):
abort(401)
jobs.put(event) # sent once and never retried: acknowledge first, work after
return "", 204
def reply_on_instagram(event: dict, text: str) -> None:
c, me = credentials(event["account_id"]), event["external_id"]
for m in event["entry"].get("messaging", []): # DMs, minus echoes of your own
if "message" in m and not m["message"].get("is_echo"):
graph(c, f"{me}/messages", recipient={"id": m["sender"]["id"]}, message={"text": text})
for ch in event["entry"].get("changes", []): # comments, minus your own replies
if ch["field"] == "comments" and ch["value"]["from"]["id"] != me:
graph(c, f"{ch['value']['id']}/replies", message=text)Python වලින් TikTok වලට post කරන්නේ කෙසේද?
easyCoexistence හි MCP tool publish_tiktok_post හරහා. TikTok හි API for Business වීඩියෝවක් video_url ලෙස ගන්නා අතර, 2023 නොවැම්බර් 16 සිට, token ඇති developer යෙදුම සඳහා සත්යාපනය කළ domain හෝ URL prefix එකකින් පමණක් එය බාගනී; එය easyCoexistence හිමිය. tool එකට ඔබේ HTTPS link එක ලබා දෙන විට, එය TikTok වෙත easyCoexistence හි සත්යාපිත media route එක මත අත්සන් කළ address එකක් ලබා දෙයි, file එක ගබඩා නොකර stream කරමින්. Photo posts වලට image_urls ලබා දිය යුතුය, JPEG හෝ WebP file 1 සිට 35 දක්වා, සහ get_tiktok_publish_status මගින් TikTok සැකසෙන අතරතුර post එක අනුගමනය කරයි.
Python වලින් එය, Python 3.10 හෝ ඊට පසු, version 2 official MCP SDK හරහා එක call එකකි. https://easycoexistence.com/api/mcp server එක OAuth 2.1 පමණක් පිළිගන්නා බැවින්, SDK හි OAuthClientProvider ඔබේ client ලියාපදිංචි කර, PKCE සමඟ එක browser පුරනය වීමක් ධාවනය කර tokens නැවුම් කරයි. Access tokens පැයක් ද refresh tokens දින 60ක් ද වලංගු වන අතර, සෑම refresh එකකින්ම නව යුගලයක් නිකුත් වේ; ඒවා ගබඩා කරන storage මගින් backend එක පුරනය වී තබයි.
import asyncio
import os
from urllib.parse import parse_qs, urlparse
import httpx2
from pydantic import AnyUrl
from mcp import Client
from mcp.client.auth import AuthorizationCodeResult, OAuthClientProvider
from mcp.client.streamable_http import streamable_http_client
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
MCP_URL = "https://easycoexistence.com/api/mcp"
class Storage: # keep both values in a database, so the browser sign-in happens once
tokens: OAuthToken | None = None
client: OAuthClientInformationFull | None = None
async def get_tokens(self): return self.tokens
async def set_tokens(self, tokens): self.tokens = tokens
async def get_client_info(self): return self.client
async def set_client_info(self, info): self.client = info
async def show_sign_in(url: str) -> None:
print("Sign in to easyCoexistence:", url)
async def read_callback() -> AuthorizationCodeResult:
query = parse_qs(urlparse(input("Paste the address you were sent back to: ")).query)
return AuthorizationCodeResult(code=query["code"][0], state=query["state"][0])
oauth = OAuthClientProvider(
server_url=MCP_URL,
client_metadata=OAuthClientMetadata(
client_name="My social agent",
redirect_uris=[AnyUrl("http://localhost:3030/callback")],
scope="mcp",
),
storage=Storage(),
redirect_handler=show_sign_in,
callback_handler=read_callback,
)
async def main() -> None:
timeout = httpx2.Timeout(30.0, read=300.0)
async with httpx2.AsyncClient(auth=oauth, timeout=timeout) as http:
async with Client(streamable_http_client(MCP_URL, http_client=http)) as client:
result = await client.call_tool("publish_tiktok_post", {
"account_id": os.environ["TIKTOK_CHANNEL_ID"], # the Partner API channel id
"video_url": "https://cdn.example.com/launch.mp4",
"caption": "New drop #behindthescenes",
})
print(result.content[0].text) # a share_id for get_tiktok_publish_status
asyncio.run(main())Python AI agent එකක් මෙම ගිණුම් භාවිතා කරන්නේ කෙසේද?
එකම MCP session එක හරහා. server එක ඔබේ ගිණුමේ network වල tools ලැයිස්තුගත කරන අතර, MCP tools පිළිගන්නා ඕනෑම agent framework එකකට ඒවා ඔවුන්ගේ model වෙත ලබා දිය හැකිය: එක එක network මත publish කිරීම, posts ලැයිස්තුගත කිරීම, comments කියවා ඒවාට reply කිරීම, Instagram සහ Facebook හි DM වලට reply කිරීම, comment-to-DM rules සැකසීම, සහ insights සහ analytics කියවීම. server එකේ උපදෙස් සහ එක් එක් tool එකේ විස්තරය network එකේ නීති රැගෙන යන නිසා, TikTok replies public බවත් Instagram රූප JPEG විය යුතු බවත් model එක ඉගෙනගනී.
agent එක ආරම්භ කරන්න list_social_accounts සමඟ, මන්ද අනෙක් සියලු social tools එයින් account_id ගන්නා අතර, එම id එක Partner API එක ලබා දෙන channel id එකම වේ. agent එක ක්රියාත්මක නොවන විට රාත්රී 3ට යා යුතු replies, comment rules සහ automations හරහා යයි, ඒවා easyCoexistence මත ම ක්රියාත්මක වේ.
සාමාන්ය වැරදි
- token එක config file එකක ගබඩා කිරීම. TikTok token එක දිනයක් ඇතුළත ද Google එක පැයක් ඇතුළත ද කල් ඉකුත් වන අතර, easyCoexistence එය පිටුපසින් නැවුම් කරයි; එබැවින් සෑම කාර්යයක ආරම්භයේදීම credentials endpoint එක call කරන්න.
- TikTok වලට ඔබේ domain එකේ link එකක් දීම. TikTok මාධ්ය බාගන්නේ token එක ඇති app එක සඳහා සත්යාපනය කළ URL prefix වලින් පමණි, එබැවින් publish_tiktok_post හරහා publish කරන්න, එවිට එය ඔබේ file එක easyCoexistence හි සත්යාපිත address එකෙන් serve කරයි.
- ඔබේම ඒවා ඇතුළුව, සෑම comment event එකකටම reply කිරීම. ඔබේ public replies නව comments ලෙසත් ඔබේ Instagram DM echo ලෙසත් ආපසු පැමිණෙන නිසා, author එක ගිණුමම වන events මඟ හරින්න, නැත්නම් bot එක තමන්ටම reply කරයි.
- Instagram DM එකක් අක්ෂර වලින් මැනීම. සීමාව UTF-8 බයිට් 1,000ක්, එබැවින් emoji හෝ උච්චාරණ අකුරු ඇති පෙළ ඉක්මනින් අවසන් වේ; යැවීමට පෙර encoded දිගෙන් trim කරන්න.
easyCoexistence ඔබේ Python කේතයට සහ ඔබේ AI agents වලට, WhatsApp, Instagram, Facebook, TikTok, YouTube, LinkedIn සහ තවත් channels වල පාරිභෝගිකයන් වෙත ළඟාවීමේ හැකියාව ලබා දෙයි, එක එක network හි official API හරහා. Partner API මගින් ගිණුම් සම්බන්ධ කර නව tokens ලබා දෙන අතර, MCP server මගින් agents වලට එම ගිණුම් tools ලෙස ලබා දෙයි, සහ සෑම සම්බන්ධතාවයක්ම 24/7 නිරීක්ෂණය කරයි. සම්බන්ධතාවයකට මාසිකව US$ 9 සිට US$ 0.75 දක්වා, සම්බන්ධතා එකතු කරන විට අඩුවෙයි, පළමු දින 3 නොමිලේ.
නිතර අසන ප්රශ්න
ඔබට Meta, TikTok, Google හෝ LinkedIn developer යෙදුමක් අවශ්යද?
නැත. ගිණුම් එක් එක් network මත easyCoexistence හි තමන්ගේම යෙදුම හරහා පුරනය වන අතර, credentials call එක ඔබේ කේතයට TikTok, YouTube සහ LinkedIn හි එම ගිණුමේ token එක ලබා දෙයි, network හි API එකට භාවිතා කිරීමට සූදානම්ව.
Python හි කුමන version එකද සහ libraries කීයද ඕනේ?
Partner API සහ networks වල APIs සරල HTTPS සහ JSON වේ, එබැවින් requests, httpx හෝ ඕනෑම Python HTTP client එකක් ඒවා call කරයි. TikTok publishing සහ agent tools භාවිතා කරන MCP Python SDK, version 2, Python 3.10 හෝ ඊට පසු අවශ්ය වේ.
කුමන networks මගේ endpoint එකට comments සහ DM යවයිද?
Instagram සහ Facebook comments, mentions සහ DM forward කරන අතර, TikTok comments forward කරයි, සෑම එකක්ම channel එකේ secret එකෙන් අත්සන් කර. YouTube සහ LinkedIn එවැනි events යවන්නේ නැත, එබැවින් ඔවුන්ගේ comments කාලසටහනකට අනුව ඔවුන්ගේ APIs හරහා කියවන්න.
easyCoexistence Python සඳහා කීයද?
සම්බන්ධතාවයකට මාසිකව US$ 9 සිට US$ 0.75 දක්වා, සම්බන්ධතා එකතු කරන විට අඩුවෙයි, සියලු networks මත එකම මිලකි. Partner API සහ MCP connector ඇතුළත් වන අතර seats නැත, පළමු දින 3 නොමිලේ.
තවදුරටත් කියවන්න
ඔබේ පළමු channel එක මිනිත්තු දෙකකින් සම්බන්ධ කරන්න.
වේදිකා සමාලෝචන සඳහා බලා සිටීමට අවශ්ය නැත. WhatsApp, Instagram, TikTok හෝ වෙනත් channel එකක් සම්බන්ධ කර, පසුව ඔබේ AI හට පළ කිරීමට, ප්රතිචාර දැක්වීමට හෝ වාර්තා කිරීමට පවසා එය වැඩ කරන ආකාරය බලන්න.
- දින 3ක් නොමිලේ
- ඕනෑම වේලාවක අවලංගු කරන්න
- නිල API පමණි
- 24/7 නිරීක්ෂණය කෙරේ
සත්යාපනය කළේ