Facing 403 Forbidden error in ton api

ton

#1

I am facing 403 Forbidden error while uploading file through single-chunk ton api with java code.
I am using signpost api for authentication and apache api for connection.
Below is my code snnipt for the same:

OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey,
            consumerSecret);
    oAuthConsumer.setTokenWithSecret(token, tokenSecret);
    HttpPost httpPost = new HttpPost("https://ton.twitter.com/1.1/ton/bucket/ta_data");
    byte[] bFile = readFile();
    try {
        httpPost.setHeader("Content-Type", "text/plain");
        httpPost.setHeader("HOST","ton.twitter.com");
        httpPost.setHeader("Accept-Encoding","gzip");
         httpPost.setEntity(new ByteArrayEntity(bFile));
         
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPost);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("status code::" + statusCode + ":::"
                + httpResponse.getStatusLine().getReasonPhrase());
        System.out.println("res entity::" + EntityUtils.toString(httpResponse.getEntity()));
    } catch (Exception e) {
        e.printStackTrace();
    }

Authorization is working fine because I have tested it with twitter public rest api.

Thanks in advance for help


#2

@roshan_cygnet looks like you’re missing a number of things here (at a glance X-TON-Expires and Content-Length). I highly recommend you take a look at the upload script we have here on github that show’s how to do both single and multi-chink uploads to the TON API.

Additionally, you may find this thread helpful:


#3

@brandonmblack,
As per your suggestion I changed my header values as below:
httpPost.setHeader(“Content-Type”, “text/plain”);
httpPost.setHeader(“HOST”,“ton.twitter.com”);
httpPost.setEntity(new ByteArrayEntity(bFile));
httpPost.setHeader(“Accept-Encoding”,“gzip”);
httpPost.setHeader(“X-TON-Content-Type”,“text/plain”);
httpPost.setHeader(“X-TON-Content-Length”,String.valueOf(bFile.length));
httpPost.setHeader(“X-TON-Expires”,“Sat, 13 June 2015 20:23:31 GMT”);

Here, I haven’t include Content-Length header because when we use setEntity method then it will automatically included it. So if I add it explicitly then its give me error “Content-Length header already present”.

So after including above mentioned headers still I am getting 403 Forbidden Error. Still I missed some request parameters or It is problem of Content-Length.

Thanks in advance for help.