Java
Chilkat2 Java
Twitter OAuth -- Tweet to Your Own Account
See more HTTP Examples
Demonstrates how to send a tweet (status update) to your own Twitter account using pre-known credentials, which includes:
- Consumer Key
- Consumer Secret
- Access Token
- Token Secret
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
http.setOAuth1(true);
http.setOAuthVerifier("");
http.setOAuthConsumerKey("my-consumer-key");
http.setOAuthConsumerSecret("my-consumer-secret");
http.setOAuthToken("my-access-token");
http.setOAuthTokenSecret("my-token-secret");
// Send the same status update as shown in this example:
// https://dev.twitter.com/docs/api/1.1/post/statuses/update
// IMPORTANT: Make sure this app has read/write access.
// Otherwise it cannot post an update (i.e. tweet) to the Twitter account.
HttpRequest req = new HttpRequest();
req.addParam("status", "Maybe he'll finally find his keys. #peterfalk");
req.setHttpVerb("POST");
req.setContentType("application/x-www-form-urlencoded");
HttpResponse resp = new HttpResponse();
try {
http.httpReq("https://api.twitter.com/1.1/statuses/update.json", req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (resp.getStatusCode() == 200) {
// Display the JSON response.
System.out.println(resp.getBodyStr());
} else {
System.out.println(http.getLastErrorText());
}
}
}