-
Notifications
You must be signed in to change notification settings - Fork 436
feat: Instagram Graph API integration and custom OAuth2 implementation #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a27a88d
ac569ce
58e6276
b79b192
a4502ba
9a1ae77
7055ef7
cdc2e80
63502f9
7ae8c43
9efd439
8b14aaa
856eea6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
OAuth2SchemeCredentials, | ||
SecuritySchemeOverrides, | ||
) | ||
from aci.server import config | ||
from aci.server.oauth2_manager import OAuth2Manager | ||
|
||
logger = get_logger(__name__) | ||
|
@@ -96,19 +97,39 @@ async def _get_oauth2_credentials( | |
linked_account.security_credentials | ||
) | ||
if _access_token_is_expired(oauth2_scheme_credentials): | ||
# Instagram's access token only could be refreshed with a valid access token, so we need to re-authorize if invalid | ||
if app.name == "INSTAGRAM": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just make sure to test if re-autorize works as expected when you're doing e2e testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# Since _access_token_is_expired returned True, expires_at is guaranteed to be not None | ||
actual_expires_at = oauth2_scheme_credentials.expires_at + 86400 # type: ignore[operator] | ||
if int(time.time()) > actual_expires_at: | ||
logger.error( | ||
f"Access token expired, please re-authorize, linked_account_id={linked_account.id}, " | ||
f"security_scheme={linked_account.security_scheme}, app={app.name}" | ||
) | ||
# NOTE: this error message could be used by the frontend to guide the user to re-authorize | ||
raise OAuth2Error( | ||
f"Access token expired. Please re-authorize at: " | ||
f"{config.DEV_PORTAL_URL}/appconfigs/{app.name}" | ||
) | ||
|
||
logger.warning( | ||
f"Access token expired, trying to refresh linked_account_id={linked_account.id}, " | ||
f"security_scheme={linked_account.security_scheme}, app={app.name}" | ||
) | ||
token_response = await _refresh_oauth2_access_token( | ||
app.name, oauth2_scheme, oauth2_scheme_credentials | ||
) | ||
|
||
# TODO: refactor parsing to _refresh_oauth2_access_token | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Centralize token parsing and expiry handling in OAuth2Manager The TODO is on point. Move provider-specific expiry calculation (e.g., IG’s safety margin and potential absence/presence of expires_in) into OAuth2Manager so callers don’t duplicate logic and drift over time. Return a normalized structure with access_token, refresh_token (optional), and computed expires_at. I can implement a parse_refresh_response(access_token_response: dict) -> dict[str, Any] in OAuth2Manager and update callers accordingly. Want me to open a follow-up PR? 🤖 Prompt for AI Agents
|
||
expires_at: int | None = None | ||
if "expires_at" in token_response: | ||
expires_at = int(token_response["expires_at"]) | ||
elif "expires_in" in token_response: | ||
expires_at = int(time.time()) + int(token_response["expires_in"]) | ||
if app.name == "INSTAGRAM": | ||
# Reduce expiration time by 1 day (86400 seconds) for safety margin | ||
expires_at = int(time.time()) + max(0, int(token_response["expires_in"]) - 86400) | ||
else: | ||
expires_at = int(time.time()) + int(token_response["expires_in"]) | ||
|
||
if not token_response.get("access_token") or not expires_at: | ||
logger.error( | ||
|
@@ -143,6 +164,8 @@ async def _refresh_oauth2_access_token( | |
app_name: str, oauth2_scheme: OAuth2Scheme, oauth2_scheme_credentials: OAuth2SchemeCredentials | ||
) -> dict: | ||
refresh_token = oauth2_scheme_credentials.refresh_token | ||
access_token = oauth2_scheme_credentials.access_token | ||
|
||
if not refresh_token: | ||
raise OAuth2Error("no refresh token found") | ||
|
||
|
@@ -157,9 +180,10 @@ async def _refresh_oauth2_access_token( | |
access_token_url=oauth2_scheme.access_token_url, | ||
refresh_token_url=oauth2_scheme.refresh_token_url, | ||
token_endpoint_auth_method=oauth2_scheme.token_endpoint_auth_method, | ||
custom_data=oauth2_scheme.custom_data, | ||
) | ||
|
||
return await oauth2_manager.refresh_token(refresh_token) | ||
return await oauth2_manager.refresh_token(refresh_token, access_token) | ||
|
||
|
||
def _get_api_key_credentials( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "INSTAGRAM", | ||
"display_name": "Instagram", | ||
"logo": "https://raw.githubusercontent.com/aipotheosis-labs/aipolabs-icons/refs/heads/main/apps/instagram.svg", | ||
"provider": "Meta Platforms, Inc.", | ||
"version": "1.0.0", | ||
"description": "The Instagram API allows developers to access and manage Instagram resources programmatically. It provides functionality for publishing content, retrieving user information, fetching post data, tracking user feeds, and managing direct messages through RESTful HTTP calls.", | ||
"security_schemes": { | ||
"oauth2": { | ||
"location": "header", | ||
"name": "Authorization", | ||
"prefix": "Bearer", | ||
"client_id": "{{ AIPOLABS_INSTAGRAM_APP_CLIENT_ID }}", | ||
"client_secret": "{{ AIPOLABS_INSTAGRAM_APP_CLIENT_SECRET }}", | ||
"scope": "instagram_business_basic instagram_business_content_publish instagram_business_manage_messages instagram_business_manage_comments instagram_business_manage_insights", | ||
"authorize_url": "https://www.instagram.com/oauth/authorize", | ||
"access_token_url": "https://api.instagram.com/oauth/access_token", | ||
"refresh_token_url": "https://graph.instagram.com/refresh_access_token", | ||
"custom_data": { | ||
"exchange_token_url": "https://graph.instagram.com/access_token" | ||
}, | ||
"token_endpoint_auth_method": "client_secret_post" | ||
} | ||
}, | ||
"default_security_credentials_by_scheme": {}, | ||
"categories": ["Social Media"], | ||
"visibility": "public", | ||
"active": true | ||
} |
Uh oh!
There was an error while loading. Please reload this page.