feat(social): Implement Real Meta API, Webhook Controller, Auto-Reply tests, AI Sentiment analysis and Security Rules
This commit is contained in:
45
extra-addons/mymach_social_nextgen/api/x_api.py
Normal file
45
extra-addons/mymach_social_nextgen/api/x_api.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import requests
|
||||
import base64
|
||||
from urllib.parse import urlencode
|
||||
|
||||
class XAPI:
|
||||
"""Helper class for X (Twitter) OAuth 2.0 API."""
|
||||
|
||||
AUTH_URL = "https://twitter.com/i/oauth2/authorize"
|
||||
TOKEN_URL = "https://api.twitter.com/2/oauth2/token"
|
||||
|
||||
def __init__(self, client_id, client_secret, redirect_uri):
|
||||
self.client_id = client_id
|
||||
self.client_secret = client_secret
|
||||
self.redirect_uri = redirect_uri
|
||||
|
||||
def get_auth_url(self, state, code_challenge="challenge"):
|
||||
# Note: X requires PKCE. For a complete implementation, a real code_verifier and code_challenge should be generated.
|
||||
params = {
|
||||
'response_type': 'code',
|
||||
'client_id': self.client_id,
|
||||
'redirect_uri': self.redirect_uri,
|
||||
'scope': 'tweet.read tweet.write users.read offline.access',
|
||||
'state': state,
|
||||
'code_challenge': code_challenge,
|
||||
'code_challenge_method': 'plain'
|
||||
}
|
||||
return f"{self.AUTH_URL}?{urlencode(params)}"
|
||||
|
||||
def exchange_code_for_token(self, code, code_verifier="challenge"):
|
||||
data = {
|
||||
'grant_type': 'authorization_code',
|
||||
'client_id': self.client_id,
|
||||
'redirect_uri': self.redirect_uri,
|
||||
'code': code,
|
||||
'code_verifier': code_verifier
|
||||
}
|
||||
auth_string = f"{self.client_id}:{self.client_secret}"
|
||||
b64_auth = base64.b64encode(auth_string.encode('ascii')).decode('ascii')
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': f'Basic {b64_auth}'
|
||||
}
|
||||
response = requests.post(self.TOKEN_URL, data=data, headers=headers, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
Reference in New Issue
Block a user