0

I am trying to upload a file to following the API information in this service. Easy Post API.

I am able to successfully send the first GET request with Digest authentication.

I'm getting a 403 - Unauthorized when trying to upload the file with 'PUT'.

This is the code I have. I am using a custom web client to set parameters in the web request.

public class CustomWebClient : WebClient
{

    private BingMailConfigOptions ConfigOptions;

    public CustomWebClient(BingMailConfigOptions configOptions) : base()
    {
        ConfigOptions = configOptions;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);

        request.ServicePoint.Expect100Continue = false;
        request.Method = "PUT";
        request.Credentials = GetCredentialCache(address, ConfigOptions);

        return request;
    }

    public static CredentialCache GetCredentialCache(Uri uri, BingMailConfigOptions options)
    {
        var credentialCache = new CredentialCache
        {
            {
            new Uri(uri.GetLeftPart(UriPartial.Authority)), 
            "Digest",
            new NetworkCredential(options.AuthUserName, options.AuthPassword, uri.GetLeftPart(UriPartial.Authority))
            }
        };
        return credentialCache;
    }
}

// in a separate class. 
private void Upload(string sessionId, string filePath)
{
    _log.Trace("Trying to upload the file: " + filePath);
    var file = new FileInfo(filePath);

    if (file.Exists)
    {

        using (var uploader = new CustomWebClient(ConfigOptions))
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            Uri uri = new Uri("https://bingmail.com.au/" + "direct_upload/{0}/{1}"(sessionId, HttpUtility.UrlEncode(file.Name))); 
            uploader.UploadFile(uri, "PUT", filePath);
        } 
    }
    else
    {
        throw new Exception("File Not found");
    }
}

Can you please tell me what I'm doing wrong or point me in the right direction?

Thanks

1 Answers1

1

I finally figured out a solution. Hope it will help someone someday.

Complete solution except some easy-to-figure-out methods are posted in this gist. Bing-Mail Easy Post Api - Version 1.3

What i did was modified the DigestAuthFixer from https://stackoverflow.com/a/3117042/959245 to support any HTTP method.

Then used that to create the session, when we create the session using DigestAuthFixer it stores the Digest-Auth headers which i can reuse when uploading the files.

 using (var client = new WebClient())
{
    var uri = new Uri(_easypostHosts[2] + UploadUri.FormatWith(sessionId, HttpUtility.UrlEncode(fileName)));

    // get the auth headers which are already stored when we create the session
    var digestHeader = DigestAuthFixer.GetDigestHeader(uri.PathAndQuery, "PUT");
    // add the auth header to our web client
    client.Headers.Add("Authorization", digestHeader);

    // trying to use the UploadFile() method doesn't work in this case. so we get the bytes and upload data directly 
    byte[] fileBytes = File.ReadAllBytes(filePath);

    // as a PUT request
    var result = client.UploadData(uri, "PUT", fileBytes);

    // result is also a byte[].
    content = result.Length.ToString();
} 
Community
  • 1
  • 1