You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Twitter按钮添加点击监听器并实现登录请求?

Fixing Twitter Login Request in Your Android App

Hey there! I see you’ve got the dependencies set up and initial configuration done, but the issue is you’re overriding the default click behavior of the TwitterLoginButton with your own OnClickListener—that’s why the login request isn’t firing. Let’s adjust your code to get the login flow working properly:

Step 1: Remove the Custom OnClickListener

The TwitterLoginButton is designed to handle the login trigger on its own, so your custom click listener is blocking the default action. Just delete that part entirely.

Step 2: Updated MainActivity Code

Here’s the corrected version of your code with a proper working login flow:

public class MainActivity extends AppCompatActivity {
    TwitterLoginButton loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize Twitter with your config (replace CONSUMER_KEY and CONSUMER_SECRET with your actual values!)
        TwitterConfig config = new TwitterConfig.Builder(this)
                .logger(new DefaultLogger(Log.DEBUG))
                .twitterAuthConfig(new TwitterAuthConfig("YOUR_CONSUMER_KEY", "YOUR_CONSUMER_SECRET"))
                .debug(true)
                .build();
        Twitter.initialize(config);

        // Set up the login button
        setupLoginButton();
    }

    private void setupLoginButton() {
        loginButton = findViewById(R.id.login_button);

        // Set callback to handle login success/failure
        loginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Login succeeded! Retrieve user session details
                TwitterSession session = result.data;
                String userId = String.valueOf(session.getUserId());
                String userName = session.getUserName();

                Toast.makeText(MainActivity.this, "Logged in as " + userName, Toast.LENGTH_LONG).show();
                
                // You can now use this session to make authenticated Twitter API calls
            }

            @Override
            public void failure(TwitterException exception) {
                // Handle login failure
                Toast.makeText(MainActivity.this, "Login failed: " + exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Pass the activity result to the login button to complete the flow
        loginButton.onActivityResult(requestCode, resultCode, data);
    }
}

Key Reminders:

  • Replace placeholders: Swap YOUR_CONSUMER_KEY and YOUR_CONSUMER_SECRET with the actual values from your Twitter Developer Dashboard (you get these when creating a new Twitter app).
  • No custom click handler needed: The TwitterLoginButton will automatically launch the official Twitter login screen when tapped once you’ve set the callback.
  • Success callback usage: Use the TwitterSession object to access user info (ID, username) and authenticate future API requests.

Quick Additional Check:

Ensure you have the internet permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

That’s it! Now when you tap the login button, it’ll trigger the standard Twitter login flow, and you’ll get the user’s session details in the success callback once they’ve logged in.

内容的提问来源于stack exchange,提问作者Andy Rubin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:39:54