Java Reference
In-Depth Information
HttpRequestFactory factory;
if (useOAuth)
factory = transport.createRequestFactory(
parametersProvider. getOAuthPostSignatureParameters());
else
factory = transport.createRequestFactory();
String url = API_ENDPOINT_URL + operation;
GenericUrl reqUrl = new GenericUrl(url);
UrlEncodedContent content = null;
if (parameters != null && method.equals("POST"))
content = new UrlEncodedContent(parameters);
if (parameters != null && method.equals("GET"))
for (Entry<String, String> parameter : parameters.entrySet())
reqUrl.put(parameter.getKey(), parameter.getValue());
HttpRequest req = null;
try {
req = factory.buildRequest(method, reqUrl, content);
HttpResponse resp = req.execute();
if (resp.isSuccessStatusCode()) {
return resp.parseAsString();
} else {
System.err.println("Request failed with status code: " +
resp.getStatusCode());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Again, much of the same logic is at work here. The makeRequest method allows you to send an
OAuth authenticated request to the Twitter REST service and takes parameters (and adds them to
the URL in the case of a GET request or to the HTTP request body in case of a POST request) before
firing off the request.
Note that OAuthParametersProvider is not used here. Instead, the code uses
OAuthPostSignatureParametersProvider , which is a class that still needs to be made:
package com.twitter.api;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
public class OAuthPostSignatureParametersProvider extends OAuthParametersProvider {
public OAuthPostSignatureParametersProvider(String configurationName,
String consumerKey, String consumerSecret, String requestTokenUrl,
String authorizeUrl, String accessTokenUrl,
boolean requiresVerification) {
super(configurationName, consumerKey, consumerSecret, requestTokenUrl,
Search WWH ::




Custom Search