androidtwitterandroid-twitter

How to Integrate Twitter api with My Android app using Link(url)


In my Android application I have done sharing a media(photo or video) into Facebook API using link(url),for this purpose I have used below code.what I need is I have to share the selected media(photo/video) into Twitter API using same link(url). I searched over the internet but didn't get required solution so anyone please suggest a better solution for this task.

How I integrate with Facebook
mediaUrl contains the value of link

 private UiLifecycleHelper uiHelper;
    uiHelper = new UiLifecycleHelper(this, null);
        uiHelper.onCreate(savedInstanceState); 


            try {
                    FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                            .setLink(mediaUrl)
                            .build();
                    uiHelper.trackPendingDialogCall(shareDialog.present());
                } catch (FacebookException fe){
          Toast.makeText(getApplicationContext(),"Facebook error! Most likely facebook app is not installed.", Toast.LENGTH_SHORT).show();

                }


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

            uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
                @Override
                public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                    Log.e("Activity", String.format("Error: %s", error.toString()));
                }

                @Override
                public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
                    Log.i("Activity", "Success!");
                }
            });
        }

Solution

  • EDIT:

    I think then you have to use the sdk. and I used Social Auth Lib many times.

    Download Social Auth Library and See Sample Projects for Reference

    Code Sample:

    // Global Variables
    
    SocialAuthAdapter adapter;
    
    
     // Code
    
    adapter = new SocialAuthAdapter(new ResponseListener());
    
    adapter.addProvider(Provider.TWITTER, R.drawable.twitter);
    adapter.addCallBack(Provider.TWITTER,
            "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");
    try {
        adapter.addConfig(Provider.TWITTER,
                StringUtils.TWITTER_PUBLIC_KEY,
                StringUtils.TWITTER_SECERET_KEY, null);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    adapter.authorize(Social_Login.this, Provider.TWITTER);
    
    
    /**
     * Listens Response from Library
     * 
     */
    
    private final class ResponseListener implements DialogListener {
        @Override
        public void onComplete(Bundle values) {
    
            // Variable to receive message status
            log("Social Login", "Authentication Successful");
    
            // Get name of provider after authentication
            final String providerName = values
                    .getString(SocialAuthAdapter.PROVIDER);
    
            log("Social Login", "Provider Name = " + providerName);
            toast(providerName + " connected");
    
            // Get the User Info
            adapter.getUserProfileAsync(new ProfileDataListener());
    
        }
    
        @Override
        public void onError(SocialAuthError error) {
            error.printStackTrace();
    
            log("Social Login", error.getMessage());
        }
    
        @Override
        public void onCancel() {
            log("Social Login", "Authentication Cancelled");
        }
    
        @Override
        public void onBack() {
            log("Social Login", "Dialog Closed by pressing Back Key");
        }
    }
    
     // To receive the profile response after authentication
    
    private final class ProfileDataListener implements
            SocialAuthListener<Profile> {
    
        @Override
        public void onError(SocialAuthError arg0) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onExecute(String arg0, Profile arg1) {
            // TODO Auto-generated method stub
            log("Custom-UI", "Receiving Data");
    
            Profile profileMap = arg1;
    
            toast(profileMap.getDisplayName());
    
    
           // Now Post the msg
            final EditText edit = (EditText) findViewById(R.id.editTxt);
            Button update = (Button) findViewById(R.id.update);
    
            update.setOnClickListener(new OnClickListener() { 
    
                @Override
                public void onClick(View v) {
                        adapter.updateStatus(edit.getText().toString(), new MessageListener(), false);
                }
            });
        }
    }
    
    // To get status of message after authentication
    private final class MessageListener implements SocialAuthListener<Integer> {
        @Override
        public void onExecute(String provider, Integer t) {
            Integer status = t;
            if (status.intValue() == 200 || status.intValue() == 201 || status.intValue() == 204)
                Toast.makeText(CustomUI.this, "Message posted on" + provider, Toast.LENGTH_LONG).show();
            else
                Toast.makeText(CustomUI.this, "Message not posted" + provider, Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onError(SocialAuthError e) {
    
        }
    }
    

    This answer is fine if you want to post through twitter official App...

    This code will open the twitter app for tweet, If app is installed in phone...

      // Create intent using ACTION_VIEW and a normal Twitter url: 
    
      String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s", UrlEncoder.encode("Tweet text"), UrlEncoder.encode("https://www.google.fi/"));
    
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl)); 
    
       // Narrow down to official Twitter app, if available: 
    
      List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0); 
    
      for (ResolveInfo info : matches) { 
           if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter"))   {   
                    intent.setPackage(info.activityInfo.packageName); 
            } 
        } 
       startActivity(intent);
    

    See detailed answer here