public class OAuthSignpostClient extends URLConnectionHttpClient implements Twitter.IHttpClient, java.io.Serializable
The Signpost jar is included in the JTwitter download.
Example Usage #1 (out-of-bounds, desktop based -- _not_ for Android):
OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
Twitter jtwit = new Twitter("yourtwittername", client);
// open the authorisation page in the user's browser
// This is a convenience method for directing the user to client.authorizeUrl()
client.authorizeDesktop();
// get the pin
String v = client.askUser("Please enter the verification PIN from Twitter");
client.setAuthorizationCode(v);
// Optional: store the authorisation token details
String[] accessToken = client.getAccessToken();
// use the API!
jtwit.setStatus("Messing about in Java");
Example Usage #2 (using callbacks, works on Android):
If you can handle callbacks, then this can be streamlined.
On Android, you can use Intents to launch a web page, & to catch the resulting
callback.
On a desktop, you need a webserver and a servlet (eg. use Jetty or Tomcat)
to handle callbacks.
Replace "oob" with your callback url. Direct the user to client.authorizeUrl(). Twitter will then call your callback with the request token and verifier (authorisation code).
OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, myCallbackUrl);
Twitter jtwit = new Twitter("yourtwittername", client);
URI url = client.authorizeUrl();
// Direct the user to this url!
Now we wait for the callback...
HttpServletRequest request = from your servlet;
// get the pin
String verifier = request.getParameter("oauth_verifier");
client.setAuthorizationCode(verifier);
// The client is now good for use. But wait: if you get an access token
// and secret, you can store them for next time:
String[] accessTokenAndSecret = client.getAccessToken();
// Then you can in future use
// OAuthSignpostClient client = new OAuthSignpostClient(APP_KEY, APP_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
// use the API!
jtwit.setStatus("Messing about in Java");
There are alternative OAuth libraries you can use:
OAuthHttpClient
,
OAuthScribeClient
,
Serialized FormModifier and Type | Field and Description |
---|---|
static java.lang.String |
JTWITTER_OAUTH_KEY
Deprecated.
Please register an app with Twitter and use your own key!
This consumer key (and secret) allows you to get up and running fast. However you are strongly advised to register your own app at http://dev.twitter.com Then use your own key and secret. This will be less confusing for users, and it protects you incase the JTwitter key gets changed. |
static java.lang.String |
JTWITTER_OAUTH_SECRET
For use with
JTWITTER_OAUTH_KEY |
name, timeout
Constructor and Description |
---|
OAuthSignpostClient(java.lang.String consumerKey,
java.lang.String consumerSecret,
java.lang.String callbackUrl)
This is for doing the initial authorisation.
|
OAuthSignpostClient(java.lang.String consumerKey,
java.lang.String consumerSecret,
java.lang.String accessToken,
java.lang.String accessTokenSecret)
Use this if you already have an accessToken for the user.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
askUser(java.lang.String question)
Convenience method for desktop apps only - does not work in
Android
|
void |
authorizeDesktop()
Deprecated.
|
java.net.URI |
authorizeUrl()
Call Twitter to start the authorisation flow.
|
boolean |
canAuthenticate()
Whether this client is setup to do authentication when contacting the
Twitter server.
|
URLConnectionHttpClient |
clone()
Identical to
URLConnectionHttpClient.copy() |
Twitter.IHttpClient |
copy() |
java.lang.String[] |
getAccessToken() |
OAuthConsumer |
getConsumer() |
AbstractOAuthProvider |
getProvider() |
HttpParameters |
getProviderResponseParams()
Provide low level access to the provider -- e.g.
|
java.net.HttpURLConnection |
post2_connect(java.lang.String uri,
com.winterwell.json.JSONObject body)
Lower-level POST method - stringifies JSON body instead of encoding vars
|
java.net.HttpURLConnection |
post2_connect(java.lang.String uri,
java.util.Map<java.lang.String,java.lang.String> vars)
Lower-level POST method.
|
java.lang.String |
postMultipartForm(java.lang.String url,
java.util.Map<java.lang.String,?> vars) |
protected void |
setAuthentication(java.net.URLConnection connection)
Set a header for basic authentication login.
|
void |
setAuthorizationCode(java.lang.String verifier)
Set the authorisation code (aka the verifier).
|
void |
setAuthorizationCode(java.lang.String verifier,
java.lang.String oauth_token,
java.lang.String oauth_token_secret)
Set the authorisation code (aka the verifier).
|
void |
setFlickrProvider() |
void |
setFoursquareProvider() |
void |
setLinkedInProvider()
Replace the default Twitter urls with the LinkedIn urls.
|
void |
setName(java.lang.String name)
Unlike the base class
URLConnectionHttpClient , this does not set
name by default. |
void |
setProvider(AbstractOAuthProvider provider)
Set to Twitter settings by default.
|
java.lang.String |
signInUrl() |
checkRateLimit, connect, delete, disconnect, getHeader, getPage, getRateLimit, getRateLimits, isRetryOnError, post, post2_connect, post2_getPayload, postJSON, processHeaders, setGzip, setHtmlImpliesError, setMinRateLimit, setRetryOnError, setTimeout, toString, updateRateLimits
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
connect, delete, getHeader, getPage, getRateLimit, getRateLimits, isRetryOnError, post, post2_connect, postJSON, setRetryOnError, setTimeout
public static final java.lang.String JTWITTER_OAUTH_KEY
This consumer key (and secret) allows you to get up and running fast. However you are strongly advised to register your own app at http://dev.twitter.com Then use your own key and secret. This will be less confusing for users, and it protects you incase the JTwitter key gets changed.
public static final java.lang.String JTWITTER_OAUTH_SECRET
JTWITTER_OAUTH_KEY
public OAuthSignpostClient(java.lang.String consumerKey, java.lang.String consumerSecret, java.lang.String callbackUrl)
consumerKey
- consumerSecret
- callbackUrl
- Servlet that will get the verifier sent to it, or "oob" for
out-of-band (user copies and pastes the pin to you)public OAuthSignpostClient(java.lang.String consumerKey, java.lang.String consumerSecret, java.lang.String accessToken, java.lang.String accessTokenSecret)
consumerKey
- consumerSecret
- accessToken
- accessTokenSecret
- public OAuthConsumer getConsumer()
public final java.lang.String postMultipartForm(java.lang.String url, java.util.Map<java.lang.String,?> vars) throws TwitterException
uri
- vars
- Can include File valuesTwitterException
public static java.lang.String askUser(java.lang.String question)
Convenience method for desktop apps only - does not work in Android
Opens a popup dialog asking the user to enter the verification code. (you would then callsetAuthorizationCode(String)
). This is only
relevant when using out-of-band instead of a callback-url. This is a
convenience method -- you will probably want to build your own UI around
this.
This method requires Swing. It will not work on Android devices!
question
- e.g. "Please enter the authorisation code from Twitter"public AbstractOAuthProvider getProvider()
@Deprecated public void authorizeDesktop()
This method requires Swing. It will not work on Android!
authorizeUrl()
public java.net.URI authorizeUrl()
signInUrl();
public java.lang.String signInUrl()
signInUrl();
public boolean canAuthenticate()
Twitter.IHttpClient
Twitter_Account.verifyCredentials()
if you need to check a
login.canAuthenticate
in interface Twitter.IHttpClient
canAuthenticate
in class URLConnectionHttpClient
public Twitter.IHttpClient copy()
copy
in interface Twitter.IHttpClient
copy
in class URLConnectionHttpClient
public URLConnectionHttpClient clone()
URLConnectionHttpClient
URLConnectionHttpClient.copy()
clone
in class URLConnectionHttpClient
public java.lang.String[] getAccessToken()
public java.net.HttpURLConnection post2_connect(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars) throws java.io.IOException, OAuthException
Twitter.IHttpClient
post2_connect
in interface Twitter.IHttpClient
post2_connect
in class URLConnectionHttpClient
java.io.IOException
OAuthException
public java.net.HttpURLConnection post2_connect(java.lang.String uri, com.winterwell.json.JSONObject body) throws java.io.IOException, OAuthException
Twitter.IHttpClient
post2_connect
in interface Twitter.IHttpClient
post2_connect
in class URLConnectionHttpClient
java.io.IOException
OAuthException
protected void setAuthentication(java.net.URLConnection connection)
URLConnectionHttpClient
setAuthentication
in class URLConnectionHttpClient
public void setAuthorizationCode(java.lang.String verifier) throws TwitterException
verifier
- a pin code which Twitter gives the user (with the oob method),
or which you get from the callback response as the parameter
"oauth_verifier".java.lang.RuntimeException
- throws an exception if the verifier is invalidTwitterException
public void setAuthorizationCode(java.lang.String verifier, java.lang.String oauth_token, java.lang.String oauth_token_secret) throws TwitterException
verifier
- a pin code which Twitter gives the user (with the oob method),
or which you get from the callback response as the parameter
"oauth_verifier".java.lang.RuntimeException
- throws an exception if the verifier is invalidTwitterException
public void setFoursquareProvider()
public void setLinkedInProvider()
public void setFlickrProvider()
public void setName(java.lang.String name)
URLConnectionHttpClient
, this does not set
name by default. But you can set it for nicer error messages.name
- public void setProvider(AbstractOAuthProvider provider)
provider
- Note: These objects are NOT thread safe.setLinkedInProvider()
,
setFoursquareProvider()
public HttpParameters getProviderResponseParams()