Friendships/create api gives 403 error always



I have used this api, I pass Authenticated token also but still api result is same 403. what chages should i make???

My code to call api is below:

public String getTwitterFollow(String url) {

    String results = null;
    // Step 1: Encode consumer key and secret
    try {
        // URL encode the consumer key and secret
        String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8");
        String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8");

        // Concatenate the encoded consumer key, a colon character, and the
        // encoded consumer secret
        String combined = urlApiKey + ":" + urlApiSecret;

        // Base64 encode the string
        String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP);

        // Step 2: Obtain a bearer token
        HttpPost httpPost = new HttpPost(TwitterTokenURL);
        httpPost.setHeader("Authorization", "Basic " + base64Encoded);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
        String rawAuthorization = getResponseBody(httpPost);
        Authenticated auth = jsonToAuthenticated(rawAuthorization);

        // Applications should verify that the value associated with the
        // token_type key of the returned object is bearer
        if (auth != null && auth.token_type.equals("bearer")) {

            url = url.replaceAll(" ", "%20");

            // Step 3: Authenticate API requests with bearer token
            HttpPost httpGet = new HttpPost(url);

            // construct a normal HTTPS request and include an Authorization
            // header with the value of Bearer <>
            httpGet.setHeader("Authorization", "Bearer " + auth.access_token);
            httpGet.setHeader("Content-Type", "application/json");

            StringEntity stringEntity;

            JSONObject obj=new JSONObject();


            stringEntity = new StringEntity(obj.toString());
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

            // update the results with the body of the response
            results = getResponseBody(httpGet);

            System.out.println("results returns" + results);

    } catch (Exception ex) {
    return results;


Have you considered using a library like Twitter4J that will do all of the OAuth encoding for you?


I am using this code for twitter other api’s like and get response also, But for this api , I am getting 403 error


This is almost certainly related to OAuth, which is why I suggested using an existing library that would make things easier for you. Are you sure you’re encoding the request correctly with the additional parameters? Do you have a user context here - getting trends would work with app-only auth, but a follow request would need a user context…


I am passing auth access token while requesting for the api, I have shown the method through which request is passing and result is getting, Can you please suggest what is wrong in this method or Is there any link or sample example through which i can figure out the error???

I have try google for the same but not able to figure out. So Please suggest any document/sample for android. I need auto follow functionality when user login through twitter, my twitter account should automatically get followed.


Please read the automation rules. A user should always be given the choice to follow, not be forced into it.


Thanks for the quick response, It was a client requirement but now I will co-ordinate with them for this issue.