Friendships/create api gives 403 error always

api

#1

I have used this api https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true, 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();

            obj.put("screen_name","NameofUser");
            obj.put("follow","true");

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

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

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

        }
    } catch (Exception ex) {
    }
    return results;
}

#2

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


#3

I am using this code for twitter other api’s like https://api.twitter.com/1.1/trends/place.json?id=1 and get response also, But for this api https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true , I am getting 403 error


#4

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…


#5

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.


#6

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


#7

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