Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ jobs:
BLUESKY_HANDLE: ${{ secrets.BLUESKY_HANDLE }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
run: |
#In order to create posts on the test accounts remove the --debugposters debug flag
python ./main.py --debugsources --debugposters
python ./main.py --debugsources
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def create_posters(debug=False):
return [PosterDebug()]

from social_posters.instagram import PosterInstagram
from social_posters.bluesky import PosterBluesky
# from social_posters.bluesky import PosterBluesky

posters = []
posters.append(PosterBluesky())
# posters.append(PosterBluesky())
posters.append(PosterInstagram())
return posters

Expand Down
24 changes: 22 additions & 2 deletions social_posters/instagram.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time

import requests

Expand All @@ -22,6 +23,7 @@ def platform_name(self) -> str:

def authenticate(self) -> bool:
if not self._is_available:
print("Instagram: credentials not set (INSTAGRAM_BUSINESS_ACCOUNT_ID or INSTAGRAM_PAGE_ACCESS_TOKEN missing)")
return False
try:
response = requests.get(
Expand All @@ -32,7 +34,13 @@ def authenticate(self) -> bool:
response.raise_for_status()
self._authenticated = True
return True
except Exception:
except requests.exceptions.HTTPError as exc:
body = exc.response.text if exc.response is not None else "no response body"
print(f"Instagram auth failed (HTTP {exc.response.status_code}): {body}")
self._authenticated = False
return False
except Exception as exc:
print(f"Instagram auth failed: {exc}")
self._authenticated = False
return False

Expand All @@ -51,14 +59,25 @@ def publish(self, post: Post) -> PostResult:

try:
container_id = self._create_media_container(post)
# Instagram needs time to process the uploaded image before publishing.
# Publishing immediately returns "Media ID is not available" (error 9007).
time.sleep(10)

media_id = self._publish_media(container_id)
return PostResult(
success=True,
post_id=media_id,
post_url="https://www.instagram.com/cute.pets.boston/",
)
except requests.exceptions.HTTPError as exc:
body = exc.response.text if exc.response is not None else "no response body"
error = f"Instagram publish failed (HTTP {exc.response.status_code}): {body}"
print(error)
return PostResult(success=False, error_message=error)
except Exception as exc:
return PostResult(success=False, error_message=str(exc))
error = f"Instagram publish failed: {exc}"
print(error)
return PostResult(success=False, error_message=error)

def _create_media_container(self, post: Post) -> str:
"""Create a media container and return its ID."""
Expand All @@ -75,6 +94,7 @@ def _create_media_container(self, post: Post) -> str:
response.raise_for_status()
return response.json()["id"]


def _publish_media(self, container_id: str) -> str:
response = requests.post(
f"{GRAPH_API_BASE}/{self.account_id}/media_publish",
Expand Down
16 changes: 16 additions & 0 deletions test_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Test script to verify Meta/Instagram API access."""

import requests

USER_TOKEN = "EAAeTXquCfa0BQZCslgXMt0CCMZATqzmOtf9BZAvROb16VDOdqONm4g3hnJ9iYnZAa2LSwmqlB4AB56Wd0dQkGlgQP5ZCBclci4CHEOkgpZAVMNVLq7f6NXQ301GPuk9PPEH0EenVdAJdxwjKqgHbw8ASBVkUtfZBN9qxJg9tAZBrlNpHKyMpvfY9iDFBvPDhwStnhQGePZAQ0BSYaUtiBho7i0SwJLmC3mjYBZCIDIoQ22QBBZBostZCLxtaZAobx5mGNPSEoq9XTeZCA3ZAodUhtyEk4iwEq5N1gZDZD"
PAGE_ID = "987663194437618"

# Step 1: Query the page directly for linked Instagram account
print("=== Instagram Business Account ===")
ig_url = f"https://graph.facebook.com/v21.0/{PAGE_ID}"
response = requests.get(ig_url, params={
"fields": "instagram_business_account,name",
"access_token": USER_TOKEN,
})
print("Status:", response.status_code)
print(response.json())
Loading