Sending direct messages in Android

android
directmessages
api

#1

Hi all!

I am trying to send a direct message using the StatusesService update method, but I am always getting the following error:

com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 403

As far as I know, that error is launched when the action is forbidden. However, I have set the permissions of my application as: read, write and access Direct Messages.

What I am doing wrong?

P.D: This is the code that I am using to try to post a direct message to someone:

// If it is a direct message, the text must be varied
            if(type.equals("private_message")){ status = "D @" + screenName + status; }

            // Send the request
            TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
            Call<Tweet> call = twitterApiClient.getStatusesService()
                    .update(status, tweetId, false, null, null, null, null, null, null);
            call.enqueue(new Callback<Tweet>() {
                @Override
                public void success(Result<Tweet> result) {  
                            Toast.makeText(getActivity(), getString(R.string.twitter_privatemessagesuccess), Toast.LENGTH_LONG).show();
                     getActivity().finish();
                }

                @Override
                public void failure(TwitterException exception) {
                    Toast.makeText(getActivity(), exception.toString(), Toast.LENGTH_LONG).show();
                }
            });

Thanks in advance


#2

You should not use that method to send Direct Messages as the D + text status update syntax is being retired. You will need to use the RestApiClient to call the DM endpoints directly.


#3

Sorry, but I do not find any function in the Javadocs documentation that allows me that. Could you be more specific?


#4

Read about extending TwitterApiClient here -> https://dev.twitter.com/twitterkit/android/access-rest-api


#5

Thanks!

For people that could have the same issue, I solved it creating the following class:

public class TwitterPrivateMessageApiClient extends TwitterApiClient {
    public TwitterPrivateMessageApiClient (TwitterSession session){
        super(session);
    }

    /** This class provides the customized PrivateMessageService */
    public PrivateMessageService getPrivateMessageService(){
        return getService(PrivateMessageService.class);
    }
}

interface PrivateMessageService {
    @FormUrlEncoded
    @POST("/1.1/direct_messages/new.json?" +
            "tweet_mode=extended&include_cards=true&cards_platform=TwitterKit-13")
    Call<Tweet> sendPrivateMessage(@Field("user_id") Long userId,
                       @Field("screen_name") String screenName,
                       @Field("text") String text);
}

And calling it from:

TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
                TwitterPrivateMessageApiClient twitterPrivateMessageApiClient = new TwitterPrivateMessageApiClient(session);
                Call<Tweet> call = twitterPrivateMessageApiClient.getPrivateMessageService().sendPrivateMessage(userId,replyName,status);
                call.enqueue(new Callback<Tweet>() {
                    @Override
                    public void success(Result<Tweet> result) {
                        Toast.makeText(getActivity(), getString(R.string.twitter_privatemessagesuccess), Toast.LENGTH_LONG).show();
                        getActivity().finish();
                    }

                    @Override
                    public void failure(TwitterException exception) {
                        Toast.makeText(getActivity(), exception.toString(), Toast.LENGTH_LONG).show();
                    }
                });

#6

@VLawliet
Hi bro, Thanks for your share.
I am very new to android…
I have fixed up many stuffs like twitter login, fet followers… but Can’t fix up this send message stuff…
TwitterPrivateMessageApiClient.java

should be so many import class… can u pls share it with me…
I am getting all class not found, like Filed, POST, Call everything…
Thanks for evrything