How to follow a person from the registered twitter account in the device using Twitter SDK

android
fabric
oauth

#1

I am working on a project wherein i have a requirement to follow a person using Twitter. For that purpose I am using Twitter SDK along with the OAuth ConsumerKey, ConsumerSecret, AccessToken and AccessTokenSecret. Everything works fine but I am completely stuck up with the things to follow the user based on the registered twitter account in the device.

What I tried

I have tried to follow a user based on the OAuth tokens generated for my application from the apps.twitter.com. Please find the below method that I am using to follow a person

private void doTwitterTask() {

        screenName = "takeoffandroid";
        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("cgxxxxxxxxxwtk")
                .setOAuthConsumerSecret("9h3xxxxxxxxxxXAKG")
                .setOAuthAccessToken("3890xxxxxxxxxxJLa2nED")
                .setOAuthAccessTokenSecret("Q3Hxxxxxx5PK");

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        try {
            twitter.createFriendship(screenName);
        } catch (TwitterException e) {
            Log.d("MCG","Error");
        }
    }

The above method works fine but the follow count is submitted from the main account where i have registered the application from apps.twitter.com not from the device registered user account. Please help me how to proceed with my requirement. Any kind of hint and tips would be helpful to me. Thanks in advance.


#2

Are you using Fabric, or Twitter4J? This looks more like Twitter4J code.

I’ve edited out your tokens, you should avoid sharing those publically as it would enable other developers to hijack your tokens and pretend to be your app (which would potentially impact your rate limits).

By providing a full set of tokens like this, you’re not retrieving the tokens for the user on the device. You’ll need to run a sign-in with Twitter flow and then use the access tokens for that user with your friendship call.

Note that autofollowing without the user’s awareness or consent is not permitted, so your app will need to provide very clear opt-in instructions before running this method (I realise that this is just test code at this point).


#3

Thanks for your reply @andypiper. I know there is a high risk of hijacking the profile using OAuth tokens and In fact even I am more concern about it . The tokens which I posted is not an actual token that I am using in my project and it was generated only meant for the testing purpose and that too I have completely edited and updated the test project consumer key, consumer secret, access token and access token secret on my own by replacing with a random characters before posting. I haven’t even changed the character length of all tokens because to rise my issue more clear in the public forums. Thanks for your solutions and suggestions. I really appreciate it.

Note: Kindly unblock my account. I haven’t done anything wrong in the twitter community. Being a developer I know the rules and principles that should be followed in the public forums. I have many followers and reputations in the social forums. Please consider my concern. Thanks in advance.