mardi 4 août 2015

Android: "400 bad request" error when trying to tweet with media using Fabric and Twitter REST API

I am trying to build an app where the user can post to Twitter with the possibility to attach images. I am using Fabric for Twitter integration and for the Tweeting I am trying to extend the API client to make an improved version of the Statuses Service's update method. I don't want to use Tweet Composer for tweeting with images, as I want to avoid the plus UI call and I also want to be able to include location in the tweets.

Here is my custom Twitter API client class:

public class MyTwitterApiClient extends TwitterApiClient {

    public MyTwitterApiClient(TwitterSession session)
    {
        super(session);
    }

    public StatusWithMediaService getStatusWithMediaService() {
        return  getService(StatusWithMediaService.class);
    }


}

interface StatusWithMediaService {
    @FormUrlEncoded
    @POST(value="/1.1/statuses/update.json")
    void update(@Field(value="status")
                java.lang.String status,
                @Field(value="in_reply_to_status_id")
                java.lang.Long inReplyToStatusId,
                @Field(value="possibly_sensitive")
                java.lang.Boolean possiblySensitive,
                @Field(value="lat")
                java.lang.Double latitude,
                @Field(value="long")
                java.lang.Double longitude,
                @Field(value="place_id")
                java.lang.String placeId,
                @Field(value="display_cooridnates")
                java.lang.Boolean displayCoordinates,
                @Field(value="trim_user")
                java.lang.Boolean trimUser,
                @Field(value ="media_ids")
                String mediaIds,
                com.twitter.sdk.android.core.Callback<Tweet> cb);
}

And here is the part of my code where I am trying to tweet (as for now I am using a static value for the media entity - the ID of a picture I uploaded using twurl):

submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (eventtype != null && mypos != null) {

                    MyTwitterApiClient myclient = new MyTwitterApiClient(Twitter.getSessionManager().getActiveSession());

                    StatusWithMediaService mediaService = myclient.getStatusWithMediaService();

                    mediaService.update("@tkl_testi " + eventtype + ": \n" + comment.getText().toString(), null, false, mypos.latitude, mypos.longitude, null, true, false, "628524774898180096", new Callback<Tweet>() {
                        @Override
                        public void success(Result<Tweet> tweetResult) {
                            Toast.makeText(getActivity(), "Raportin lähettäminen onnistui.", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void failure(TwitterException e) {
                            Log.e("testapp", e.getMessage());
                        }
                    });
                }
                else
                {
                    Toast.makeText(getActivity(), "Puuttuvat tiedot: sijainti", Toast.LENGTH_SHORT).show();
                }
            }
        });

When I run the app and I try to tweet, I get the following error message: 400 Bad Request

What is the problem? What should I do differently? I am quite new to Fabric, Twitter REST API and Retrofit, so I would appreciate advice from more experienced members :) Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire