Advanced Twitter

Advanced Use
The following section is not required if you are simply using the Action Bar

Linking Users with Twitter

If you are not using the ActionBar or want to manually link a user to their Twitter account you can do so with the link method

// The "this" argument refers to the current Activity 
TwitterUtils.link(this, new SocializeAuthListener() {

	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAuthSuccess(SocializeSession session) {
		// User was authed.
	}
	
	@Override
	public void onAuthFail(SocializeException error) {
		// Handle error
	}
	
	@Override
	public void onError(SocializeException error) {
		// Handle error
	}
});

If you already have Twitter integration in your app and do not wish to replace this with the Socialize implementation you can still link the user to Twitter within Socialize by providing the user’s Twitter auth token and secret

// The user's Twitter auth token
String twToken = "ABCDEF...GHIJKL";
//The user's Twitter auth token secret
String twSecret = "ABCDEF...GHIJKL";

TwitterUtils.link(this, twToken, twSecret, new SocializeAuthListener() {

	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAuthSuccess(SocializeSession session) {
		// User was authed.
	}
	
	@Override
	public void onAuthFail(SocializeException error) {
		// Handle error
	}
	
	@Override
	public void onError(SocializeException error) {
		// Handle error
	}
});

Unlinking Users from Twitter

To unlink a user from their Twitter account simple call unlink. This call executes synchronously.

// Disconnect the user from their Twitter account
TwitterUtils.unlink(this);	

Posting to Twitter

By default all actions created using the SDK will provide the end user with the opportunity to share their action with Twitter (if Twitter has been configured) however it is also possible to manually post content to a user’s Twitter feed.

Posting Socialize Entities to Twitter

The simplest and most effective use of Twitter within Socialize is to post entities. This approach takes full advantage of Socialize SmartDownloads which helps maximize the effectiveness of your social strategy.

To post an entity to a user’s Twitter feed simply call the tweetEntity method.

Entity entity = Entity.newInstance("http://myentity.com", "My Name");
	
TwitterUtils.tweetEntity(this, entity, "Text to be posted", new SocialNetworkShareListener() {
	
	@Override
	public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
		// Handle error
	}
	
	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
		// Called after the post returned from Twitter.
		// responseObject contains the raw JSON response from Twitter.
	}
	
	@Override
	public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
		// Called just prior to the post.
		// postData contains the dictionary (map) of data to be posted.  
		// You can change this here to customize the post.
		// Return true to prevent the post from occurring.
		return false;
	}
});

Posting Directly to Twitter

If you want complete control over what is posted to Twitter, Socialize provides a simple interface to the Twitter REST API.

Refer to the Twitter REST API documentation for more specific implementation information

http://dev.twitter.com/docs/api

Sending a Tweet

// Create a Tweet object
	
Tweet tweet = new Tweet();

tweet.setText("Test Message");

// Execute a POST on twitter
// The "this" argument refers to the current Activity
TwitterUtils.tweet(this, tweet, new SocialNetworkListener() {
	
	@Override
	public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
		// Handle error
	}
	
	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
		// Called after the post returned from Twitter.
		// responseObject contains the raw JSON response from Twitter.
	}
	
	@Override
	public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
		// Called before the post to the given network is made
		// Return true to prevent the post from occurring
		return false;
	}
});

Executing a POST

// The API path to be called
String graphPath = "statuses/update.json";

// The data to be posted. This is based on the API endpoint
// See https://dev.twitter.com/docs/api
Map<String, Object> postData = new HashMap<String, Object>();
postData.put("status", "A message to post");
	
// Execute a POST on twitter
// The "this" argument refers to the current Activity
TwitterUtils.post(this, graphPath, postData, new SocialNetworkPostListener() {
	
	@Override
	public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
		// Handle error
	}
	
	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
		// Called after the post returned from Twitter.
		// responseObject contains the raw JSON response from Twitter.
	}
});

If you want to take full advantage of Socialize SmartDownloads and post the auto-generated SmartDownload urls for your entity or app you can do this by creating a simple share object without propagation first.

// Create a simple share object to get the propagation data
final Entity entity = Entity.newInstance("http://myentity.com", "My Name");

ShareOptions options = ShareUtils.getUserShareOptions(this);

// The "this" argument refers to the current Activity
ShareUtils.registerShare(this, entity, options, new ShareAddListener() {
	
	@Override
	public void onError(SocializeException error) {
		// Handle error
	}
	
	@Override
	public void onCreate(Share share) {
		
		// Get the propagation info from the result
		PropagationInfoResponse propagationInfoResponse = share.getPropagationInfoResponse();
		
		PropagationInfo propagationInfo = propagationInfoResponse.getPropagationInfo(SocialNetwork.TWITTER);
		
		// Tweet the link
		Tweet tweet = new Tweet();
		tweet.setText("A message to post " + propagationInfo.getEntityUrl()); // Use the SmartDownload URL
			
		// Execute a POST on twitter
		TwitterUtils.tweet(context, tweet, new SocialNetworkListener() {
			
			@Override
			public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
				// Handle error
			}
			
			@Override
			public void onCancel() {
				// The user cancelled the operation.
			}
			
			@Override
			public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
				// Called after the post returned from Twitter.
				// responseObject contains the raw JSON response from Twitter.
			}
			
			@Override
			public boolean onBeforePost(Activity parent, SocialNetwork socialNetwork, PostData postData) {
				// Called before the post to the given network is made
				// Return true to prevent the post from occurring
				return false;
			}
		});		
	}
}, SocialNetwork.TWITTER);

Executing a GET

// The graph API path to be called
String graphPath = "followers/ids.json";

// Execute a GET on twitter
// The "this" argument refers to the current Activity
TwitterUtils.get(this, graphPath, null, new SocialNetworkPostListener() {
	
	@Override
	public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
		// Handle error
	}
	
	@Override
	public void onCancel() {
		// The user cancelled the operation.
	}
	
	@Override
	public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
		// Called after the post returned from Twitter.
		// responseObject contains the raw JSON response from Twitter.
	}
});

Posting Photos to Twitter

You can easily post photo’s to a user’s Twitter Photos using the tweetPhoto method on TwitterUtils

//The "this" argument refers to the current Activity
final Activity context = this;
	
final Entity entity = Entity.newInstance("http://myentity.com", "My Name");	
	
// First create a Socialize share object so we get the correct URLs
ShareOptions options = ShareUtils.getUserShareOptions(context);

ShareUtils.registerShare(context, entity, options, new ShareAddListener() {
	
	@Override
	public void onError(SocializeException error) {
		// Handle error
	}
	
	@Override
	public void onCreate(Share result) {
		
		// We have the result, use the URLs to add to the post
		PropagationInfo propagationInfo = result.getPropagationInfoResponse().getPropagationInfo(ShareType.TWITTER);
		String link = propagationInfo.getEntityUrl();

		// TODO: Get the URI of your image from the local device.
		// TODO: ***** DON'T FORGET TO USE YOUR OWN IMAGE HERE (See the sample app for a working example) ****
		Uri photoUri = null;

		// Format the picture for Twitter
		try {
			byte[] imageData = TwitterUtils.getImageForPost(context, photoUri);
			
			// Create a photo tweet
			PhotoTweet tweet = new PhotoTweet();
			
			// Add the photo to the post
			tweet.setImageData(imageData);
			
			// Add the link returned from Socialize to use SmartDownloads
			tweet.setText("A test photo of something " + link);
			
			// Post to twitter
			TwitterUtils.tweetPhoto(context, tweet, new SocialNetworkPostListener() {
				
				@Override
				public void onNetworkError(Activity context, SocialNetwork network, Exception error) {
					// Handle error
				}
				
				@Override
				public void onCancel() {
					// The user cancelled the auth process
				}
				
				@Override
				public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
					// The post was successful
				}
			});
		}
		catch (IOException e) {
			// Handle error
		}
	}
}, SocialNetwork.TWITTER);