I'm working with an API that uses OAuth-based authentication, accepts dynamic JSON payloads, and processes requests asynchronously. After submitting a request, I poll for job status updates. However, I'm encountering an issue where the job status never updates from "pending" even though the server logs show the job completes successfully.
Expected behavior: After submitting the job, I should be able to poll the job status endpoint and receive an updated status such as "completed" or "failed".
Observed behavior: The job status remains "pending" indefinitely, despite the backend processing the job correctly.
Minimal reproducible code:
import requests
# Step 1: Authenticate
token = requests.post("https://api.example.com/oauth/token", data={
"client_id": "my_client_id",
"client_secret": "my_client_secret",
"grant_type": "client_credentials"
}).json()["access_token"]
headers = {"Authorization": f"Bearer {token}"}
# Step 2: Submit job
job_response = requests.post("https://api.example.com/jobs", headers=headers, json={
"task": "process_data",
"parameters": {"input": "some_input_data"}
}).json()
job_id = job_response["job_id"]
# Step 3: Poll job status
status_response = requests.get(f"https://api.example.com/jobs/{job_id}/status", headers=headers).json()
print("Job status:", status_response["status"])
What could be causing the job status to remain "pending"? Are there common pitfalls with asynchronous APIs or OAuth that I should check?
status_responseonly once directly after the job was submitted. So it's quite likely that it's not finished yet. But then you poll never again. How do you expect thestatus_responseto change, if you don't do another request? Maybe you could add a loop that requests the status once in a while until it changes?retry-afterheader on the 202 response, assuming they are using 202. Some api designers are quite friendly and provide sufficient information to make follow up calls, particularly for async calls. check for this header and if it exists, retry the status polling after the indicated time.