📜 ⬆️ ⬇️

Custom login button on the Twitter SDK (Fabric kit)

This article is intended for those who have already figured out how to install and configure the Twitter Login SDK ( Fabric kit ), but are stuck on the question of how to implement a custom ui for the login button. The introductory article on Fabric can be found at the link .

“Finally I’ll get rid of this cumbersome code,” I thought, cutting out twitter4j and anticipating how accurate the authorization classes would be thanks to the Twitter SDK. It was on a Saturday morning. According to my calculations, this should take no more than an hour. In the evening I cursed that morning fool who decided to sacrifice a whole day off for a little refactoring.

What happened? First of all, do not believe that the Fabric plugin will do everything for you. Quite the contrary - you still cry with him. So that those who solve the login problem, I advise in parallel glancing here .

With the TwitterLoginButton widget that sdk provides, everything is clear, but what do those who are not comfortable with the standard design do? Long hours of googling did not give anything. The complete absence of api references, java doc or source code also did not simplify the task.
')
In the end I found such a crutch:

private void loginToTwitter() { TwitterCore.getInstance().logIn(this, new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> twitterSessionResult) { Log.i(LOG_TAG, "success"); } @Override public void failure(TwitterException e) { Log.i(LOG_TAG, "failure"); } }); } 

And to make it all work (oh, horror!):

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); (new TwitterLoginButton(this)).onActivityResult(requestCode, resultCode, data); } 

After a couple of days, having spat enough, I found the strength to find a normal solution. Everything turned out to be quite simple:

  private TwitterAuthClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig)); Button customLoginButton = (Button) findViewById(R.id.custom_twitter_login); customLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { client = new TwitterAuthClient(); client.authorize(LoginActivity.this, new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> twitterSessionResult) { Toast.makeText(LoginActivity.this, "success", Toast.LENGTH_SHORT).show(); } @Override public void failure(TwitterException e) { Toast.makeText(LoginActivity.this, "failure", Toast.LENGTH_SHORT).show(); } }); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); client.onActivityResult(requestCode, resultCode, data); } 

If you have more recent information on the sdk documentation, please contact me - I’ll be happy to add it to the article.

Github source code: github.com/Defuera/Social-network-login

Source: https://habr.com/ru/post/250907/


All Articles