Android的分享Tweet消息不显示在Twitter上墙消息、Android、Tweet、Twitter

2023-09-07 02:23:58 作者:黎明、独徘徊

我知道我有aske dthis的问题,但我没有得到任何满意的解决方案为止还没有,我在微博注册获取用户密钥和秘密密钥,我得到的登录,但是当我更新Twitter上的任何消息这表明中小企业的信息已经更新,但是当我在我的twitterid是checkinh,我没有得到任何消息。

i know i have aske dthis question,but i am not getting any satisfaction solution till yet.,i registered in twitter for getting consumer key,and secret key.,i am getting login ,but when i update any message on twitter it show sme message has been updated,but when i am checkinh in my twitterid.,i am not getting any message.

public class MainActivity extends Activity {// Constants
  static String TWITTER_CONSUMER_KEY = ""; // place your cosumer key here
static String TWITTER_CONSUMER_SECRET = ""; // place your consumer secret here

// Preference Constants
static String PREFERENCE_NAME = "twitter_oauth";
static final String oauth_token = "";
static final String oauth_token_secret = "";
static final String PREF_KEY_TWITTER_LOGIN = "";

static final String TWITTER_CALLBACK_URL = "";

// Twitter oauth urls
static final String URL_TWITTER_AUTH = "oauth_autherize";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";

// Login button
private   Button btnLoginTwitter;
// Update status button
private   Button btnUpdateStatus;
// Logout button
private  Button btnLogoutTwitter;
// EditText for update
private    EditText txtUpdate;
// lbl update
private   TextView lblUpdate;
private  TextView lblUserName;

// Progress dialog
ProgressDialog pDialog;

// Twitter
private static Twitter twitter;
private static RequestToken requestToken;
private AccessToken accessToken;

// Shared Preferences
private static SharedPreferences mSharedPreferences;

// Internet Connection detector
private ConnectionDetector cd;

// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // Check if twitter keys are set
    if(TWITTER_CONSUMER_KEY.trim().length() == 0 || TWITTER_CONSUMER_SECRET.trim().length() == 0){
        // Internet Connection is not present
        alert.showAlertDialog(MainActivity.this, "Twitter oAuth tokens", "Please set your twitter oauth tokens first!", false);
        // stop executing code by return
        return;
    }

    // All UI elements
    btnLoginTwitter = (Button) findViewById(R.id.btnLoginTwitter);
    btnUpdateStatus = (Button) findViewById(R.id.btnUpdateStatus);
    btnLogoutTwitter = (Button) findViewById(R.id.btnLogoutTwitter);
    txtUpdate = (EditText) findViewById(R.id.txtUpdateStatus);
    lblUpdate = (TextView) findViewById(R.id.lblUpdate);
    lblUserName = (TextView) findViewById(R.id.lblUserName);

    // Shared Preferences
    mSharedPreferences = getApplicationContext().getSharedPreferences(
            "MyPref", 0);
    btnLoginTwitter.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Call login twitter function
            loginToTwitter();
        }
    });

    btnUpdateStatus.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Call update status function
            // Get the status from EditText
            String status = txtUpdate.getText().toString();
            new updateTwitterStatus().execute(status);

        } 

    });
    btnLogoutTwitter.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Call logout twitter function
            logoutFromTwitter();
        }
    });

    if (!isTwitterLoggedInAlready()) {
        Uri uri = getIntent().getData();
        if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
            // oAuth verifier
            final String verifier = uri
                    .getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

            try {

                Thread thread = new Thread(new Runnable(){
                    @Override
                    public void run() {
                        try {

                            // Get the access token
                            MainActivity.this.accessToken = twitter.getOAuthAccessToken(
                                    requestToken, verifier);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();

                // Shared Preferences
                Editor e = mSharedPreferences.edit();
                e.putString(oauth_token, accessToken.getToken());
                e.putString(oauth_token_secret,
                        accessToken.getTokenSecret());
                // Store login status - true
                e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                e.commit(); // save changes

                Log.e("Twitter OAuth Token", "> " + accessToken.getToken());

                // Hide login button
                btnLoginTwitter.setVisibility(View.GONE);

                // Show Update Twitter
                lblUpdate.setVisibility(View.VISIBLE);
                txtUpdate.setVisibility(View.VISIBLE);
                btnUpdateStatus.setVisibility(View.VISIBLE);
                btnLogoutTwitter.setVisibility(View.VISIBLE);

                // Getting user details from twitter
                // For now i am getting his name only
                long userID = accessToken.getUserId();
                User user = twitter.showUser(userID);
                String username = user.getName();

                // Displaying in xml ui
                lblUserName.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
            } catch (Exception e) {
                // Check log for login errors
                Log.e("Twitter Login Error", "> " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

}
private void loginToTwitter() {
    // Check if already logged in
    if (!isTwitterLoggedInAlready()) {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
        builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
        twitter4j.conf.Configuration configuration = builder.build();

        TwitterFactory factory = new TwitterFactory(configuration);
        twitter = factory.getInstance();


        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {

                    requestToken = twitter
                            .getOAuthRequestToken(TWITTER_CALLBACK_URL);
                    MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse(requestToken.getAuthenticationURL())));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();         
    } else {
        // user already logged into twitter
        Toast.makeText(getApplicationContext(),
                "Already Logged into twitter", Toast.LENGTH_LONG).show();
    }
}

class updateTwitterStatus extends AsyncTask<String, String, String> {

    private int statusId;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Updating to twitter...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        Log.d("Tweet Text", "> " + args[0]);
        String status = args[0];
        try {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

            // Access Token 
            String access_token = mSharedPreferences.getString(oauth_token,"");
            // Access Token Secret
            String access_token_secret = mSharedPreferences.getString(oauth_token_secret, "");

            AccessToken accessToken = new AccessToken(access_token, access_token_secret);

            Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);

            // Update status
            twitter4j.Status response = twitter.updateStatus(status);

            Log.d("Status", "> " + response.getText());
        } catch (TwitterException e) {
            // Error in updating status
            Log.d("Twitter Update Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Status tweeted successfully", Toast.LENGTH_SHORT)
                        .show();
                txtUpdate.setText("");
            }
        });
    }

}

private void logoutFromTwitter() {
    // Clear the shared preferences
    Editor e = mSharedPreferences.edit();
    e.remove(oauth_token);
    e.remove(oauth_token_secret);
    e.remove(PREF_KEY_TWITTER_LOGIN);
    e.commit();

    btnLogoutTwitter.setVisibility(View.GONE);
    btnUpdateStatus.setVisibility(View.GONE);
    txtUpdate.setVisibility(View.GONE);
    lblUpdate.setVisibility(View.GONE);
    lblUserName.setText("");
    lblUserName.setVisibility(View.GONE);

    btnLoginTwitter.setVisibility(View.VISIBLE);
}

private boolean isTwitterLoggedInAlready() {
    // return twitter login status from Shared Preferences
    return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
}

protected void onResume() {
    super.onResume();
}

}在这里,我得到这个形象,当我得到logedin ..

} here i am getting this image.,when i get logedin..

推荐答案

我想你忘记添加 OAuth用户键秘密资产 oauth_consumer.properties文件,你可以通过注册与Twitter应用程序中获取。他们捆绑键,这样就可以快速测试,但强烈建议您更改这些密钥。首先,它是一个安全问题,为您的应用,其次有时我们的钥匙给错误,因为太多的开发人员正在测试。

I think you forget to add the OAuth consumer keys and secrets into Assets in oauth_consumer.properties file, that you can get by registering your application with twitter. They have bundled keys so that you can test quickly, but it is strongly recommended that you change these keys. First, it is a security issue for your application and secondly sometimes our keys give errors because too many developers are testing.

SocialAuth Android是受欢迎的 SocialAuth Java库的Andr​​oid版本。现在,如果你想与多个社交网络应用程序集成,你并不需要集成多个的SDK 。你只需要在你的应用程序中的 SocialAuth 的Andr​​oid库集成之后添加code的几行。回到这个socialauth-机器人。一个最好的方法来整合所有社交媒体。

SocialAuth Android is an Android version of popular SocialAuth Java library. Now you do not need to integrate multiple SDKs if you want to integrate your application with multiple social networks. You just need to add few lines of code after integrating the SocialAuth Android library in your app. Go to this socialauth-android. One of the best approach to integrate all social media.

转到此链接,更好地了解 socialauth-的Andr​​oid 。也,code在 Github上

Go to this link for better understanding socialauth-android .also, code available in Github