apify / apify-sdk-python

The Apify SDK for Python is the official library for creating Apify Actors in Python. It provides useful features like actor lifecycle management, local storage emulation, and actor event handling.
https://docs.apify.com/sdk/python
Apache License 2.0
115 stars 11 forks source link

Python Client Hello World Fails with https status of 401 "apify_client._errors.ApifyApiError: User was not found or authentication token is not valid" #183

Closed bs7280 closed 6 months ago

bs7280 commented 7 months ago

TLDR - Hello world python example fails with auth error, debugging http request I don't see my auth token sent anywhere

Hello,

I got an issue with a real apify scraper so I tried the hello world and am getting the same auth error

Thanks, Ben

See details below:

Code:

from apify_client import ApifyClient

# Initialize the ApifyClient with your API token
client = ApifyClient("apify_api_....")

# Prepare the Actor input
run_input = {
    "first_number": 1,
    "second_number": 2,
}

# Run the Actor and wait for it to finish
run = client.actor("apify/python-example").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(item)

Error:

apify_client._errors.ApifyApiError: User was not found or authentication token is not valid

I also did a debugger breakpoint in the _BaseHttpClient() class on line 169 in method _make_request() and have this information below. Biggest thing that sticks out to me is the API token is not sent anywhere? Unless im looking in the wrong spot

(Pdb) request.url
URL('https://api.apify.com/v2/acts/apify~python-example/runs')
(Pdb) request.headers
Headers({'host': 'api.apify.com', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'accept': 'application/json, */*', 'user-agent': 'ApifyClient/1.6.2 (darwin; Python/3.11.6); isAtHome/False', 'authorization': '[secure]', 'content-type': 'application/json; charset=utf-8', 'content-encoding': 'gzip', 'content-length': '54'})

Extra info:

Full Traceback

Traceback (most recent call last):
  File "/Users/benshaughnessy/code/py_zillow/apify_test.py", line 13, in <module>
    run = client.actor("apify/python-example").call(run_input=run_input)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/_logging.py", line 82, in wrapper
    return fun(resource_client, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/clients/resource_clients/actor.py", line 266, in call
    started_run = self.start(
                  ^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/_logging.py", line 82, in wrapper
    return fun(resource_client, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/clients/resource_clients/actor.py", line 219, in start
    response = self.http_client.call(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/_http_client.py", line 195, in call
    return retry_with_exp_backoff(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/_utils.py", line 64, in retry_with_exp_backoff
    return func(stop_retrying, attempt)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/benshaughnessy/code/py_zillow/venv/lib/python3.11/site-packages/apify_client/_http_client.py", line 193, in _make_request
    raise ApifyApiError(response, attempt)
apify_client._errors.ApifyApiError: User was not found or authentication token is not valid
vdusek commented 6 months ago

Hi @bs7280, thank you for reporting the problem. I tried replicating it with my API token and it seems to be working fine.

vdusek commented 6 months ago

Closing it since we haven't received any responses from the questioner. If this is still a problem, let's reopen it.