Unauthorized response from Server with API upload

Posted by Ethan Shafer on Ask Ubuntu See other posts from Ask Ubuntu or by Ethan Shafer
Published on 2012-11-05T18:25:41Z Indexed on 2012/11/22 23:10 UTC
Read the original article Hit count: 298

Filed under:

I'm writing a library in C# to help me develop a Windows application. The library uses the Ubuntu One API. I am able to authenticate and can even make requests to get the Quota (access to Account Admin API) and Volumes (so I know I have access to the Files API at least) Here's what I have as my Upload code:

    public static void UploadFile(string filename, string filepath)
    {
        FileStream file = File.OpenRead(filepath);
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);

        RestClient client = UbuntuOneClients.FilesClient();
        RestRequest request = UbuntuOneRequests.BaseRequest(Method.PUT);
        request.Resource = "/content/~/Ubuntu One/" + filename;
        request.AddHeader("Content-Length", bytes.Length.ToString());
        request.AddParameter("body", bytes, ParameterType.RequestBody);

        client.ExecuteAsync(request, webResponse => UploadComplete(webResponse));
    }

Every time I send the request I get an "Unauthorized" response from the server.

For now the "/content/~/Ubuntu One/" is hardcoded, but I checked and it is the location of my root volume. Is there anything that I'm missing?

UbuntuOneClients.FilesClient() starts the url with "https://files.one.ubuntu.com" UbuntuOneRequests.BaseRequest(Method.{}) is the same requests that I use to send my Quota and Volumes requests, basically just provides all of the parameters needed to authenticate.

EDIT:: Here's the BaseRequest() method:

    public static RestRequest BaseRequest(Method method)
    {
        RestRequest request = new RestRequest(method);
        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        request.AddParameter("realm", "");
        request.AddParameter("oauth_version", "1.0");
        request.AddParameter("oauth_nonce", Guid.NewGuid().ToString());
        request.AddParameter("oauth_timestamp", DateTime.Now.ToString());
        request.AddParameter("oauth_consumer_key", UbuntuOneRefreshInfo.UbuntuOneInfo.ConsumerKey);
        request.AddParameter("oauth_token", UbuntuOneRefreshInfo.UbuntuOneInfo.Token);
        request.AddParameter("oauth_signature_method", "PLAINTEXT");
        request.AddParameter("oauth_signature", UbuntuOneRefreshInfo.UbuntuOneInfo.Signature);
        //request.AddParameter("method", method.ToString());

        return request;
    }

and the FilesClient() method:

    public static RestClient FilesClient()
    {
        return (new RestClient("https://files.one.ubuntu.com"));
    }

© Ask Ubuntu or respective owner

Related posts about ubuntu-one-api