Java Reference
In-Depth Information
Great, everything works. However, the code is contained in one huge main method and thus could
use some cleaning up. You'll also need to find a way to parse the received JSON, and it would be
better to avoid users having to grant permission to your application each time it is run, as you can
just as well reuse the same access token for future requests.
Start by creating an OAuthParametersProvider class, which you can use to perform a three‐step
OAuth authentication, and will be able to store access tokens for later use. First, create a new
folder (not a source folder) in your RESTWithJava project called tokens . Next, enter the following
code:
package com.twitter.api;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthGetAccessToken;
import com.google.api.client.auth.oauth.OAuthGetTemporaryToken;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
public class OAuthParametersProvider {
protected final String configurationName;
protected final String consumerKey;
protected final String consumerSecret;
protected final String requestTokenUrl;
protected final String authorizeUrl;
protected final String accessTokenUrl;
protected final boolean requiresVerification;
protected final HttpTransport transport;
protected final OAuthHmacSigner signer;
public OAuthParametersProvider(String configurationName,
String consumerKey, String consumerSecret,
String requestTokenUrl, String authorizeUrl, String accessTokenUrl,
Search WWH ::




Custom Search