Java Reference
In-Depth Information
// Step 2: User grants access
// ---------------------------
// Construct an authorization URL using the temporary request token
OAuthAuthorizeTemporaryTokenUrl authorizeUrl =
new OAuthAuthorizeTemporaryTokenUrl(AUTHORIZE_URL);
authorizeUrl.temporaryToken = requestTokenResponse.token;
// We ask the user to open this URL and grant access
// Twitter includes an extra safety measure, asks the user to provide PIN
String pin = null;
System.out.println("Go to the following link:\n" + authorizeUrl.build());
InputStreamReader converter = new InputStreamReader(System.in, "UTF-8");
BufferedReader in = new BufferedReader(converter);
while (pin == null) {
System.out.println("Enter the verification PIN provided by Twitter:");
pin = in.readLine();
}
// Step 3: Request the access token the user has approved
// ------------------------------------------------------
// Get the access token
// We need to provide our application key
// The signer, the transport objects
// The temporary request token
// And a verifier string (the PIN number provided by Twitter)
OAuthGetAccessToken accessToken = new OAuthGetAccessToken(ACCESS_TOKEN_URL);
accessToken.consumerKey = CONSUMER_KEY;
accessToken.signer = signer;
accessToken.transport = transport;
accessToken.temporaryToken = requestTokenResponse.token;
accessToken.verifier = pin;
// Get back our access token
OAuthCredentialsResponse accessTokenResponse = accessToken.execute();
System.out.println("Access Token:");
System.out.println("- oauth_token = " + accessTokenResponse.token);
System.out.println("- oauth_token_secret = " +
accessTokenResponse.tokenSecret);
// Update the signer again
// We now replace the temporary request token with the final access token
signer.tokenSharedSecret = accessTokenResponse.tokenSecret;
// Set up OAuth parameters which can now be used in authenticated requests
OAuthParameters parameters = new OAuthParameters();
parameters.consumerKey = CONSUMER_KEY;
parameters.token = accessTokenResponse.token;
parameters.signer = signer;
// OAuth steps finished, we can now start accessing the service
// ------------------------------------------------------------
HttpRequestFactory factory = transport.createRequestFactory(parameters);
Search WWH ::




Custom Search