Java IPFS Infura error 401 - Unauthorized

I`m tring to connect with ipfs infura, but send me the response code 401
I created an IPFS project and I have my projectId and secretKey

protected void onCreate(Bundle savedInstanceState) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    url = new URL("");

                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

                    String baseAuthStr = "projectId" + ":" + "secretKey";
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.addRequestProperty("Authorization", "Basic " + Arrays.toString(Base64.encode(baseAuthStr.getBytes(), Base64.NO_WRAP)));

//                    ipfsClient = new IPFS(IPFS_INFURA_URL);


                } catch (MalformedURLException | ProtocolException e) {
                } catch (IOException exception) {


Hello @gabriel , can you try to generate the needed base64 string from the CLI for example and then pass it to your conn.addRequestProperty?

You can use the following to generate the string:
echo -n "ProjectID:ProjectSecret" | base64 -w 0
Then use it as such in conn.addRequestProperty(“Authorization”, “Basic base64string”).

Found this stackoverflow resource with several possible answers on how to create basic auth with java. Maybe it helps.
Also can you kindly share the full error you are receiving?


@radu After numerous attempts, it still gives error 401

1 Like

Hey @gabriel , I have managed to successfully connect to Infura IPFS using one of the examples in the stack overflow link I shared in my earlier reply. Java is not my strongest point, but please see the below code snippet example for an HttpBasicAuth class:

import java.util.Base64;

public class HttpBasicAuth {

    public static void main(String[] args) {

        try {
            URL url = new URL ("");
            String baseAuthStr = "27hXXXLRsj" + ":" + "205XXX15b";
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty  ("Authorization", "Basic " + Base64.getEncoder().encodeToString(baseAuthStr.getBytes()));
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   =
                new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
        } catch(Exception e) {

Below is the successful result of running the code, which displays the CID QmYt9ypyGsR1BKdaCGPdwdBgAiuXK5AYN2bGSNZov7YXuk

Kindly note that I have used the cat API endpoint which shows the IPFS object data. More info on the cat API can be found here.

Also find below a link of the CID used in the code:

Don’t forget to use your own Project ID and Secret when building the baseAuthStr string.

Hope this helps!

1 Like

@radu stranger, using exacly url and using my PROJECT ID and API KEY SECRET, continuos send the same error

I need install a something in my computer or a specific url?

In the really, what means QmYt9ypyGsR1BKdaCGPdwdBgAiuXK5AYN2bGSNZov7YXuk

1 Like

Hey. let’s try to troubleshoot this within a support ticket. Please raise one here Contact Us | Infura, by selecting Customer Support from the dropdown, also please specify the email associated with your Infura account.

QmYt9ypyGsR1BKdaCGPdwdBgAiuXK5AYN2bGSNZov7YXuk is the Content Identifier (CID) of an object stored in IPFS.
I can send you some introductory material on IPFS and how you can make use of of your Infura IPFS project.

1 Like

@radu Maybe if I explain the context better you can help me better.

I’m creating an application where we intend to upload video and photo files via ipfs. We chose infura to do this service

Searching how to upload this, I came to this tutorial

It didn’t work, so I got the answer that I needed to send http headers to be able to make the previous connection before using the IPFS API for java

As I understand it, I need to connect to the server, so for that, before trying to upload a file, I’m testing if my https connection is successful

This is the way?

1 Like

Hey @gabriel , all requests sent to the Infura Ipfs api need to be authenticated, but in order to upload a file you will need to use the POST method. By using GET you will receive a 405 method not allowed error, as get is not supported.
So basically in the same call you could authenticate your project and upload a file.
But a 401 error code means that you are not passing the authentication header correctly. Have you tried the code example I shared and just replacing the Project ID and Project Secret with yours?

From what I see there’s little support in the community for the java ipfs http client, as opposed to the javascript ipfs http client for which there’s plenty of material.

1 Like

@radu Have you tried the code example I shared and just replacing the Project ID and Project Secret with yours? yes…

*url = new URL("");*
*                   *
*                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();*

*                    String values = "myProjectId:myKeySecret;*
*                    byte[] encodedBytes = Base64.encode(values.getBytes(),Base64.NO_WRAP);*

*                    conn.setRequestMethod("POST");*
*                    conn.setRequestProperty("Content-Type", "application/json");*

*                    conn.addRequestProperty("Authorization", "Basic " + encodedBytes);*

*                    conn.connect();*

this code return for me error 401… base64.encode is how we can transform string in base64 in Java Android

1 Like

Do you have access to a Mac or linux terminal? let’s try to generate the base64 string manually with the following command:
echo -n "myProjectId:myKeySecret" | base64 -w 0
echo -n "27hXXX7LRsj:205XXX15b" | base64 -w 0

The resulting string would look like the below and let’s pass that one directly in the basic auth, without declaring the values string.

Something like the below:

conn.addRequestProperty("Authorization", "Basic MjdoTTc1aDhRZXXXXXXXXXXXXXXXXXXXXXXXX2FhNzk2MDg1MzJmOTJkMTVi" );*

Let’s see if it works like this.

1 Like

i’m accessing from a windows PC.

I will try.

1 Like

you can use the Chrome developers console to get the base64 encoded string in windows.
navigate to the developers tools console and type btoa("myProjectId:myKeySecret"), the result will be the string you need:

1 Like

Hi @radu. After of use this link and convert my crendentials in base 64 and use direct works.

The question right now is, how create my CID, cause this CID its yours, right?

From what I understand, I will have to create a folder, which will give me this CID, and with this cid I will upload my files that I need for the application

1 Like

Hi Gabriel, happy that you managed to connect.
I am not fluent in java, but now that you have managed to open the connection using your Infura IPFS credentials, I think that you should be able to use the tutorial you were following to continue defining your IPFS java client and use its methods to upload files or folders to IPFS and get their CIDs. My guess is that the IPFS java client should have an add file method which you could use.
I can share some documentation which can maybe get you up to speed with IPFS.

Also the below is an example of how you would add files using the ipfs-http-client javascript library. As I was mentioning earlier, my guess is that the java client should have a similar method.

1 Like

Yes, there is the “add file” method, but it keeps giving the same error when I use

ipfsClient = new IPFS(IPFS_INFURA_URL);

Thank you for all support, but i will try for another way

1 Like