简体   繁体   中英

Twitter Android-FirebaseUI auth: How to post on user's behalf once I have the access Token and the access secret on Android App??are those enough?

I logged in successfully and got the twitter access token and the twitter access secret using firebase-ui-auth [ https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md][1] :

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
       //twitter access token
        response.getIdpToken();
        //twitter access token secret
        response.getIdpSecret();
    }
}

I want to post on user's behalf(to their accounts, not to mine) using these two tokens that I will save on shared preferences.

1) Are these two tokens enough to post to user's account? 2) How do I do to post something using these two tokens?. I can't seem to find the proper docs for my particular case, the twitter api handling for android is really poor.

I already solved it myself by using fabric and its TweetComposer class.....

first you need to initialize fabric on the bootstrap Class of your app

Fabric.with(this, new Twitter(authConfig));

then on the class you want to make the tweet you get the firebase instance to get the logged in user and then you set the twitter consumer key and secret that you got when you log in to firebase UI https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md , for future reference to get the two tokens needed to tweet on user's behalf you can do it like the link specifies:

To retrieve the ID token that the IDP returned, you can extract an IdpResponse from the result Intent.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        IdpResponse idpResponse = IdpResponse.fromResultIntent(data);
        startActivity(new Intent(this, WelcomeBackActivity.class)
                .putExtra("my_token", idpResponse.getIdpToken()));
    }
}

Twitter also returns an AuthToken Secret which can be accessed with idpResponse.getIdpSecret().

and now you have everything you need:

    mAuth = FirebaseAuth.getInstance();

            if (mAuth.getCurrentUser() != null) {
                // already signed in

                twitter_consumer_key= preferences.getString("TWITTER_CONSUMER_KEY","");
                twitter_consumer_secret= preferences.getString("TWITTER_CONSUMER_SECRET","");

                TwitterAuthConfig authConfig =  new TwitterAuthConfig(twitter_consumer_key, twitter_consumer_secret);
         //setting up fabric       
         Fabric.with(this, new TwitterCore(authConfig), new TweetComposer());


            }

and then let's say I want to tweet from a custom button onClick:

        ImageButton tweetButton= (ImageButton) findViewById(R.id.tweet_button);

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

                TweetComposer.Builder builder = new TweetComposer.Builder(mContext)
                        .text("just setting up my Fabric.");
                builder.show();

            }
        });

the app will redirect you to the twitter app with the preseted message "just setting up my Fabric.". You can add pictures and videos too!

Hope that this helps someone in the future cause there is little info about fabric....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM