richardschneider / net-ipfs-http-client

InterPlanetary File System client for .Net (C#, VB, F# ...)
MIT License
158 stars 52 forks source link

Correct way how to add Project ID and Secret for Infura IPFS #67

Closed fyziktom closed 2 years ago

fyziktom commented 3 years ago

Hello,

Infura.io started beta paid version of IPFS service. I would like to use it together with this client, but I cannot find way how to add the credentials correctly. Could you please help me with some example?

I have tried to add it as part of url, but it does not work (like https://ProjetID:secret@ipfs.infura.io:5001/)

Thank you.

chrischip commented 3 years ago

I have the same issue I end up using reflection to force the authentication header into the HttpClient object:

public static IpfsClient CreateIpfsClient(string IpfsHostUrl, string IpfsHostUserName, string IpfsHostPassword ){

            var c = new IpfsClient(IpfsHostUrl);

                var httpClientInfo = typeof(IpfsClient).GetField("api", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
                var apiObj = httpClientInfo.GetValue(null);
                if (apiObj == null) {

                    MethodInfo createMethod = typeof(IpfsClient).GetMethod("Api", BindingFlags.NonPublic | BindingFlags.Instance);
                    var o = createMethod.Invoke(c, new Object[0]);
                    var httpClient = o as HttpClient;

                    var byteArray = Encoding.ASCII.GetBytes(IpfsHostUserName+ ":" + IpfsHostPassword);
                    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

                }            

            return c;
        }

something like that

fyziktom commented 2 years ago

Hello. Thank you very much for this answer. It works :)