comfyui-replicate icon indicating copy to clipboard operation
comfyui-replicate copied to clipboard

You did not pass a valid authentication token

Open formulake opened this issue 1 year ago • 12 comments

Replicate black-forest-labs/flux-pro ReplicateError Details: title: Unauthenticated status: 401 detail: You did not pass a valid authentication token

I followed the steps to set the API as instructed and tried two different tokens but it keeps throwing this back at me.

formulake avatar Sep 23 '24 14:09 formulake

Replicate black-forest-labs/flux-pro ReplicateError Details: title: Unauthenticated status: 401 detail: You did not pass a valid authentication token

I followed the steps to set the API as instructed and tried two different tokens but it keeps throwing this back at me.

Exactly the same issue and tried 2 tokens.

running on ComfyUI_windows_portable

set REPLICATE_API_TOKEN="****************************" .\python_embeded\python.exe -s ComfyUI\main.py --windows-standalone-build --disable-auto-launch pause

HTTP Request: POST https://api.replicate.com/v1/predictions "HTTP/1.1 401 Unauthorized" !!! Exception during processing !!! ReplicateError Details: title: Unauthenticated status: 401 detail: You did not pass a valid authentication token

dihan avatar Oct 07 '24 18:10 dihan

OK!! worked it out after messing about I realised that you shouldnt have quotation marks!!

Create new bat file called

run_replicate.bat

add the follwing with your api key without " "

set REPLICATE_API_TOKEN=1233987abcdefg 
.\python_embeded\python.exe -s ComfyUI\main.py --windows-standalone-build

run the file with run_replicate.bat

on Mac there is no bat files so you would need to type it into terminal. Navigate to username Comfyui % folder on terminal and copy paste the line below adding your api key

set REPLICATE_API_TOKEN=1233987abcdefg python3 main.py

on my mac its python3 but could be whatever you installed. py, python are other options

SIDE NOTE: now its working, not sure if its possible, but this would be so cool if we can run controlnets etc and then just use the processing power at replicate

dihan avatar Oct 30 '24 19:10 dihan

Hi! I work with Pinokio, and I tried EVERYTHING to add an API token, with no success so far. I tried adding it in the "ENVIRONMENT" file in Pinokio (both in the app folder and in ComfyUI folder), but I still get this error (the same as formulake,:

Replicate black-forest-labs/flux-pro ReplicateError Details: title: Unauthenticated status: 401 detail: You did not pass a valid authentication token

What did I do wrong?

DashDashDesign avatar Nov 17 '24 09:11 DashDashDesign

I finally had to create an environment variable in Windows to get this working. Hit the windows key and type environment variable to bring up the box. You want to create a new user variable with the variable name as REPLICATE_API_TOKEN and the value set to your key. On MacOS, I had to modify the ZSH file and create the environment variable. It started working fine after that.

formulake avatar Nov 17 '24 13:11 formulake

Thanks for your reply, but this doesn't work (I tried on 2 different computers). At least with Pinokio. I also modified the "ENVIRONMENT" file in Pinokio (both in the app and/or in the comfyUI folder), but still doesn't work for now... :'(

DashDashDesign avatar Nov 18 '24 08:11 DashDashDesign

nothing I do seems to work. I've ran the bat file, set environment variable, added the api key in the comfyui bat run file as mentioned above, run the set api key command via command line.... all same result... telling me I'm not authenticated

alenknight avatar Dec 21 '24 22:12 alenknight

SOLVED for my issue – First a Personal Note Thanks to a helpful Discord community member who tipped me in the right direction to a solution. Unfortunately, Replicate's official documentation is not only lacking but also contains errors. Despite opening multiple GitHub issues, Discord queries, and support tickets over the past two months, I’ve received no responses from their support staff…. Just like all the other folks that asked the same question. It's frustrating that customers must solve problems caused by poor documentation without any assistance from the company. If you're facing similar issues, don’t rely on Replicate’s official channels. Turn to the community for help—they’re the real MVPs.

Steps to Resolve Replicate API Token Issues in ComfyUI (Portable Mode)

  1. Set the Windows Environment Variable: o Add the REPLICATE_API_TOKEN as a global Windows environment variable. o Note: This global variable is not automatically picked up by ComfyUI in portable mode.
  2. Test the Environment Variable with curl: o Use a simple curl command in the terminal to confirm the variable is being picked up and the API token works: cmd Copy code curl -X GET "https://api.replicate.com/v1/account" -H "Authorization: Bearer %REPLICATE_API_TOKEN%"
  3. Verify run_gpu.bat Picks Up the Variable: o Add a debug statement in ComfyUI/main.py to confirm the environment variable is accessible: python Copy code print(f"REPLICATE_API_TOKEN: {os.getenv('REPLICATE_API_TOKEN')}")
  4. Hard Code the API Key in node.py: o Modify the replicate.Client initialization in ComfyUI/custom_nodes/comfyui-replicate/node.py: python Copy code replicate = Client( headers={ "User-Agent": "comfyui-replicate/1.0.1", "Authorization": f"Token {os.getenv('REPLICATE_API_TOKEN')}" } )

Important Notes • Updates May Overwrite Changes: o If you update either ComfyUI or its custom nodes, the changes in main.py and node.py will be overwritten. You’ll need to reapply these fixes after updates.

hope this might help some folks

alenknight avatar Dec 23 '24 08:12 alenknight

Steps to Resolve Replicate API Token Issues in ComfyUI (Portable Mode)

1. Set the Windows Environment Variable:
   o	Add the REPLICATE_API_TOKEN as a global Windows environment variable.
   o	Note: This global variable is not automatically picked up by ComfyUI in portable mode.

2. Test the Environment Variable with curl:
   o	Use a simple curl command in the terminal to confirm the variable is being picked up and the API token works:
   cmd
   Copy code
   curl -X GET "https://api.replicate.com/v1/account" -H "Authorization: Bearer %REPLICATE_API_TOKEN%"

3. Verify run_gpu.bat Picks Up the Variable:
   o	Add a debug statement in ComfyUI/main.py to confirm the environment variable is accessible:
   python
   Copy code
   print(f"REPLICATE_API_TOKEN: {os.getenv('REPLICATE_API_TOKEN')}")

4. Hard Code the API Key in node.py:
   o	Modify the replicate.Client initialization in ComfyUI/custom_nodes/comfyui-replicate/node.py:
   python
   Copy code
   replicate = Client(
   headers={
   "User-Agent": "comfyui-replicate/1.0.1",
   "Authorization": f"Token {os.getenv('REPLICATE_API_TOKEN')}"
   }
   )

I haven't had any luck getting it working even with alenknight's detailed steps. I'm pretty sure I've done step 1 because when I do step 2, the curl command, I get a response showing my Replicate username and the web address of my github account. However, I lack the coding knowledge to do step 3 so I am unsure if comfyui is actually able to access my environment variable. After doing step 4 the replicate line in my Replicate node.py (after the import lines) looks like this:

replicate = Client(headers={"User-Agent": "comfyui-replicate/1.0.1","Authorization": f"Token {os.getenv('REPLICATE_API_TOKEN')}"})

Is there some way to just hardcode my API key directly into the node.py file?

For now I will use the Replicate web interface but it would be nice to be able to use ComfyUI.

Edit: UPDATE I did indeed go ahead and hardcode my API token into the node.py file and it does work that way. I understand that this is not ideal. I believe that alenknight's code had a typo with an extra "f", but even after removing that for some reason I still can't get it to fetch an environmental variable. So here is what I have on my node.py file that I can confirm works for me. replicate = Client(headers={"User-Agent": "comfyui-replicate/1.0.1","Authorization": "Token r8_123456789"})

roofling avatar Dec 25 '24 01:12 roofling

I've had to change the way the API token gets fed to the node twice now. One stops working and the other starts. I wonder why it's so difficult for the dev team to just implement an API field in the nodes or an API loader node to enable users to plug in a token in the ComfyUI interface itself. Every other API calling node suite has it and it makes life so much easier than having to fiddle with .py files, environment variables and command/terminal line commands.

formulake avatar Dec 28 '24 03:12 formulake

I've had to change the way the API token gets fed to the node twice now. One stops working and the other starts. I wonder why it's so difficult for the dev team to just implement an API field in the nodes or an API loader node to enable users to plug in a token in the ComfyUI interface itself. Every other API calling node suite has it and it makes life so much easier than having to fiddle with .py files, environment variables and command/terminal line commands.

mine also just stopped working lol. I had to restart comfy, reapply the api... then worked. weird.

alenknight avatar Dec 31 '24 00:12 alenknight

Hey, so after almost going crazy trying to figure out what happens and the REPLICATE_API_TOKEN is reset to a blank string, and multiple failed trials and errors, I decided to search for the REPLICATE_API_TOKEN text in every file in ComfyUI's folder, because I was sure it was being reset somewhere...

Yeah, well it is, for some ... reason, from another, seemingly unrelated custom node. The custom node is called comfyui-tensorops. In its folder it has the following file: ./nodes/config.py. And this file has the following line (line 13):

os.environ["REPLICATE_API_TOKEN"] = ""

😒 😒

Not sure exactly when this is run, but it really resets the REPLICATE_API_TOKEN environment variable for the whole of Comfy UI and every node running in it. I don't know what the reason is, since it doesn't even seem to check if the variable exists beforehand, it just resets it. So I just commented out the lines above and now everything works as intended.

Of course this is a solution for those who have comfyui-tensorops installed. I have quite a few custom nodes and the only places I found the REPLICATE_API_TOKEN string is in Replicate's nodes (which is expected) and in the above node (unexpectedly). I'd still recommend for people to search for the string in their custom nodes folder if they have the same issue but without comfyui-tensorops installed.

I'll open an issue in comfyui-tensorops repo too, in case the dev wants to fix that (though I can't figure out why this specific variable, together with FAL_KEY need to be straight up reset without any notifications).

jtabox avatar Jan 03 '25 23:01 jtabox

Hi! I work with Pinokio, and I tried EVERYTHING to add an API token, with no success so far. I tried adding it in the "ENVIRONMENT" file in Pinokio (both in the app folder and in ComfyUI folder), but I still get this error (the same as formulake,:

Replicate black-forest-labs/flux-pro ReplicateError Details: title: Unauthenticated status: 401 detail: You did not pass a valid authentication token

What did I do wrong?

Don't using dns

Max-Codder avatar Feb 24 '25 08:02 Max-Codder