I'm having issues using the python client to trigg...
# ask-community
d
I'm having issues using the python client to trigger a flow. I provided the api_key and the tenant_id to the client and also tried without these inputs as I figured they would be picked up from my environment. Any ideas?
Copy code
prefect.exceptions.AuthorizationError: [{'path': ['create_flow_run'], 'message': 'AuthenticationError: Forbidden', 'extensions': {'code': 'UNAUTHENTICATED'}}]
k
Hey @Derek Heyman, does your organization have RBAC by chance? Are you using an old token? Could you try with a new key?
d
I tried creating a new key, but no luck. Our agent is hosted on an EC2 instance in AWS that has role based access to our S3 bucket where the prefect flows live
I know that the access works because I can run a flow through the UI without issue.
My python code looks like the following (removed the API key, flow id, and the tenant for security reasons):
client = Client(tenant_id='',api_key='')
client.create_flow_run(flow_id="", parameters={"stop": 1, "stop2": 2}, labels=["prefect_dev"])
This is the error I get: `/home/derek/Desktop/prefect/env/lib/python3.6/site-packages/prefect/client/client.py192 UserWarning: Found both an API token and an API key. API tokens have been deprecated and it will be ignored in favor of the API key. Remove the token from the config at `prefect.config.auth_token``
else ""
Traceback (most recent call last):
File "flow_run.py", line 15, in <module>
lambda_handler()
File "flow_run.py", line 13, in lambda_handler
client.create_flow_run(flow_id="a04b5339-30ad-4f56-9857-b3cf7815ac27", parameters={"stop": 1, "stop2": 2}, labels=["prefect_dev"])
File "/home/derek/Desktop/prefect/env/lib/python3.6/site-packages/prefect/client/client.py", line 1522, in create_flow_run
res = self.graphql(create_mutation, variables=dict(input=inputs))
File "/home/derek/Desktop/prefect/env/lib/python3.6/site-packages/prefect/client/client.py", line 561, in graphql
raise AuthorizationError(result["errors"])
prefect.exceptions.AuthorizationError: [{'path': ['create_flow_run'], 'message': 'AuthenticationError: Forbidden', 'extensions': {'code': 'UNAUTHENTICATED'}}]
k
Are you triggering a flow in another tenant? Or do you only have one tenant?
d
I only have one tenant
for tenant_id I have a string that is the name that comes after https://cloud.prefect.io/ for my cloud
Is there a way to get a list of tenants / tenant_ids?
k
You shouldn’t need to provide it. The Client will infer and get the token from local if you just do:
Copy code
from prefect.client.client import Client

client = Client() # gets logged in API key
d
yeah I tested that and I get a different error:
Copy code
INTERNAL_SERVER_ERROR
It says Flow Not Found
so I figured I was making progress when i got an authentication error instead, but maybe not
k
How do you start the agent?
d
prefect agent local start -l prefect_dev
It's running on an EC2 instance on AWS
k
Can you try
prefect agent local start -l prefect_dev -k API_KEY
and see if that helps?
Then on the flow side, just
Copy code
from prefect.client.client import Client

client = Client() # gets logged in API key
d
I'll give it a try, but just a heads up that I was able to get it working with the CLI. I'm only having issues with the python client
k
Oh is this Python client not even part of a flow?
d
yup exactly
k
Then I guess you can try
prefect auth login -k API_KEY
on the machine you are running this script?
d
the flow is separately registered to an S3 bucket
k
Is it the same when you don’t give the tenant_id to client? Just doing:
Copy code
client = Client(api_key=API_KEY)
Ohhh if you have URL like: https://cloud.prefect.io/kevin-prefect-io-s-account That is not the tenant-id. That is the tenant-slug.
You can get the id in the graphQL API:
Copy code
query {
  tenant {
    id
    slug
  }
}
So mine looks like this:
be66231b-773a-4f2d-b963-6057d3775891
d
so it looks similar to a flow id?
is there a CLI command to get tenant id?
When I don't supply the tenant_id it says flow not found. When I supply the teant_id as the tenant slug that's when I get the auth error
k
If you did
prefect auth login -key API_KEY
, then you get the ID by doing
prefect auth list-tenants
Is there a chance you are just providing the wrong Flow ID then?
Here is a flow page. Make sure you provide the flow_id instead of flow_group_id
d
There it is
thank you
I read a bit about the flow group, but figured it wasn't an issue since that same ID worked with the CLI
appreciate the help
k
Of course!