Java Reference
In-Depth Information
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
public class TwitterTest {
private static final String CONSUMER_KEY = "FILL IN YOUR API KEY";
private static final String CONSUMER_SECRET = "FILL IN YOUR SECRET KEY";
private static final String REQUEST_TOKEN_URL =
" https://api.twitter.com/oauth/request_token";
private static final String AUTHORIZE_URL =
" https://api.twitter.com/oauth/authenticate" ;
private static final String ACCESS_TOKEN_URL =
" https://api.twitter.com/oauth/access_token" ;
private static final String API_ENDPOINT_URL =
" https://api.twitter.com/1.1/statuses/home_timeline.json";
public static void main(String[] args) throws Exception {
// HttpTransport will be used to handle the HTTP requests
// This is part of the google-http library
HttpTransport transport = new NetHttpTransport();
// The OAuthHmacSigner will be used to create the oauth_signature
// Using HMAC-SHA1 as the oauth_signature_method
// The signer needs the secret key to sign requests
OAuthHmacSigner signer = new OAuthHmacSigner();
signer.clientSharedSecret = CONSUMER_SECRET;
// Step 1: Get a request token
// ---------------------------
// We need to provide our application key
// We also need to provide an HTTP transport object
// And the signer which will sign the request
OAuthGetTemporaryToken requestToken =
new OAuthGetTemporaryToken(REQUEST_TOKEN_URL);
requestToken.consumerKey = CONSUMER_KEY;
requestToken.transport = transport;
requestToken.signer = signer;
// Get back our request token
OAuthCredentialsResponse requestTokenResponse = requestToken.execute();
System.out.println("Request Token:");
System.out.println("- oauth_token = " + requestTokenResponse.token);
System.out.println("- oauth_token_secret = " +
requestTokenResponse.tokenSecret);
// Update the signer to also include the request token
signer.tokenSharedSecret = requestTokenResponse.tokenSecret;
Search WWH ::




Custom Search