90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
# connector-superoffice/discover_fields.py (Standalone & Robust)
|
|
import os
|
|
import requests
|
|
import json
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv(override=True)
|
|
|
|
# Configuration
|
|
SO_ENV = os.getenv("SO_ENVIRONMENT", "sod") # sod, stage, online
|
|
SO_CLIENT_ID = os.getenv("SO_CLIENT_ID") or os.getenv("SO_SOD")
|
|
SO_CLIENT_SECRET = os.getenv("SO_CLIENT_SECRET")
|
|
# SO_REDIRECT_URI often required for validation even in refresh flow
|
|
SO_REDIRECT_URI = os.getenv("SO_REDIRECT_URI", "http://localhost")
|
|
SO_REFRESH_TOKEN = os.getenv("SO_REFRESH_TOKEN")
|
|
|
|
def get_access_token():
|
|
"""Refreshes the access token using the refresh token."""
|
|
url = f"https://{SO_ENV}.superoffice.com/login/common/oauth/tokens"
|
|
data = {
|
|
"grant_type": "refresh_token",
|
|
"client_id": SO_CLIENT_ID,
|
|
"client_secret": SO_CLIENT_SECRET,
|
|
"refresh_token": SO_REFRESH_TOKEN,
|
|
"redirect_uri": SO_REDIRECT_URI
|
|
}
|
|
|
|
print(f"DEBUG: Refreshing token at {url} for Client ID {SO_CLIENT_ID[:5]}...")
|
|
|
|
response = requests.post(url, data=data)
|
|
if response.status_code == 200:
|
|
print("✅ Access Token refreshed.")
|
|
return response.json().get("access_token")
|
|
else:
|
|
print(f"❌ Error getting token: {response.text}")
|
|
return None
|
|
|
|
def discover_udfs(base_url, token, entity="Contact"):
|
|
"""
|
|
Fetches the UDF layout for a specific entity.
|
|
entity: 'Contact' (Firma) or 'Person'
|
|
"""
|
|
endpoint = "Contact" if entity == "Contact" else "Person"
|
|
url = f"{base_url}/api/v1/{endpoint}?$top=1&$select=userDefinedFields"
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Accept": "application/json"
|
|
}
|
|
|
|
print(f"\n--- DISCOVERING UDFS FOR: {entity} ---")
|
|
try:
|
|
response = requests.get(url, headers=headers)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if data['value']:
|
|
item = data['value'][0]
|
|
udfs = item.get('userDefinedFields', {})
|
|
|
|
print(f"Found {len(udfs)} UDFs on this record.")
|
|
|
|
# Filter logic: Show interesting fields
|
|
relevant_udfs = {k: v for k, v in udfs.items() if "marketing" in k.lower() or "robotic" in k.lower() or "challenge" in k.lower() or "ai" in k.lower()}
|
|
|
|
if relevant_udfs:
|
|
print("✅ FOUND RELEVANT FIELDS (ProgId : Value):")
|
|
print(json.dumps(relevant_udfs, indent=2))
|
|
else:
|
|
print("⚠️ No fields matching 'marketing/robotic/ai' found.")
|
|
print("First 5 UDFs for context:")
|
|
print(json.dumps(list(udfs.keys())[:5], indent=2))
|
|
else:
|
|
print("No records found to inspect.")
|
|
else:
|
|
print(f"Error {response.status_code}: {response.text}")
|
|
except Exception as e:
|
|
print(f"Request failed: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
token = get_access_token()
|
|
if token:
|
|
# Hardcoded Base URL for Cust55774 (Fix: Use app-sod as per README)
|
|
base_url = "https://app-sod.superoffice.com/Cust55774"
|
|
|
|
discover_udfs(base_url, token, "Person")
|
|
discover_udfs(base_url, token, "Contact")
|
|
else:
|
|
print("Could not get Access Token. Check .env")
|