winterwell.jtwitter
Class OAuthScribeClient

java.lang.Object
  extended by winterwell.jtwitter.OAuthScribeClient
All Implemented Interfaces:
Twitter.IHttpClient

public class OAuthScribeClient
extends java.lang.Object
implements Twitter.IHttpClient

It is recommended that you use OAuthSignpostClient instead. OAuth based login using Scribe (http://github.com/fernandezpablo85/scribe). You need version 0.6.6 of Scribe (or above)!

Example Usage (desktop based):

 
        OAuthScribeClient client = new OAuthScribeClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
        Twitter jtwit = new Twitter("yourtwittername", client);
        // open the authorisation page in the user's browser
        client.authorizeDesktop();
        // get the pin
        String v = client.askUser("Please enter the verification PIN from Twitter");
        client.setAuthorizationCode(v); 
        // use the API!
        jtwit.setStatus("Messing about in Java");
        
 

See Also:
This is the "officially supported" JTwitter OAuth client., OAuthHttpClient

Field Summary
static java.lang.String JTWITTER_OAUTH_KEY
          This consumer key (and secret) allows you to get up and running fast.
static java.lang.String JTWITTER_OAUTH_SECRET
          For use with JTWITTER_OAUTH_KEY
 
Constructor Summary
OAuthScribeClient(java.lang.String consumerKey, java.lang.String consumerSecret, java.lang.String callbackUrl)
           
OAuthScribeClient(java.lang.String consumerKey, java.lang.String consumerSecret, Token accessToken)
          Use this if you already have an accessToken for the user.
 
Method Summary
static java.lang.String askUser(java.lang.String question)
           Convenience method for desktop apps only - does not work in Android
 void authorizeDesktop()
          Redirect the user's browser to Twitter's authorise page.
 java.net.URI authorizeUrl()
           
 boolean canAuthenticate()
          Whether this client is setup to do authentication when contacting the Twitter server.
 java.net.HttpURLConnection connect(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> vars, boolean b)
          Lower-level GET method.
 Twitter.IHttpClient copy()
           
 Token getAccessToken()
           
 java.lang.String getHeader(java.lang.String headerName)
          TODO not implemented yet.
 java.lang.String getPage(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Send an HTTP GET request and return the response body.
 RateLimit getRateLimit(Twitter.KRequestType reqType)
           
 Token getRequestToken()
           
 java.lang.String post(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars, boolean authenticate)
          Send an HTTP POST request and return the response body.
 java.net.HttpURLConnection post2_connect(java.lang.String uri, java.util.Map<java.lang.String,java.lang.String> vars)
          Lower-level POST method.
 void setAuthorizationCode(java.lang.String verifier)
          Set the authorisation code (aka the verifier).
 void setRetryOnError(boolean retryOnError)
          False by default.
 void setTimeout(int millisecs)
          Deprecated. 
 void updateRateLimits(Twitter.KRequestType reqType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JTWITTER_OAUTH_KEY

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.

See Also:
Constant Field Values

JTWITTER_OAUTH_SECRET

public static final java.lang.String JTWITTER_OAUTH_SECRET
For use with JTWITTER_OAUTH_KEY

See Also:
Constant Field Values
Constructor Detail

OAuthScribeClient

public OAuthScribeClient(java.lang.String consumerKey,
                         java.lang.String consumerSecret,
                         java.lang.String callbackUrl)
Parameters:
consumerKey -
consumerSecret -
callbackUrl - Servlet that will get the verifier sent to it, or "oob" for out-of-band (user copies and pastes the pin /** Opens a popup dialog asking the user to enter the verification code. (you would then call setAuthorizationCode(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.
question - e.g. "Please enter the authorisation code from Twitter"

OAuthScribeClient

public OAuthScribeClient(java.lang.String consumerKey,
                         java.lang.String consumerSecret,
                         Token accessToken)
Use this if you already have an accessToken for the user. You can then go straight to using the API without having to authorise again.

Parameters:
consumerKey -
consumerSecret -
accessToken -
Method Detail

askUser

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 call setAuthorizationCode(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 all devices.

Parameters:
question - e.g. "Please enter the authorisation code from Twitter"
Returns:

authorizeDesktop

public void authorizeDesktop()
Redirect the user's browser to Twitter's authorise page. You will need to collect the verifier pin - either from the callback servlet, or from the user (out-of-band).

This method requires Swing. It will not work on all devices.

See Also:
authorizeUrl()

authorizeUrl

public java.net.URI authorizeUrl()
Returns:
url to direct the user to for authorisation.

canAuthenticate

public boolean canAuthenticate()
Description copied from interface: Twitter.IHttpClient
Whether this client is setup to do authentication when contacting the Twitter server. Note: This is a fast method that does not call the server, so it does not check whether the access token or password is valid. See {Twitter#isValidLogin()} or Twitter_Account.verifyCredentials() if you need to check a login.

Specified by:
canAuthenticate in interface Twitter.IHttpClient

connect

public java.net.HttpURLConnection connect(java.lang.String url,
                                          java.util.Map<java.lang.String,java.lang.String> vars,
                                          boolean b)
                                   throws java.io.IOException
Description copied from interface: Twitter.IHttpClient
Lower-level GET method.

Specified by:
connect in interface Twitter.IHttpClient
Returns:
Throws:
java.io.IOException

copy

public Twitter.IHttpClient copy()
Specified by:
copy in interface Twitter.IHttpClient
Returns:
a copy of this client. The copy can share structure, but it MUST be safe for passing to a new thread to be used in parallel with the original.

getAccessToken

public Token getAccessToken()
Returns:
the access token, if set.

getHeader

public java.lang.String getHeader(java.lang.String headerName)
                           throws java.lang.RuntimeException
TODO not implemented yet. Please see URLConnectionHttpClient for example code.

Specified by:
getHeader in interface Twitter.IHttpClient
Returns:
header value, or null if unset
Throws:
java.lang.RuntimeException

getPage

public java.lang.String getPage(java.lang.String uri,
                                java.util.Map<java.lang.String,java.lang.String> vars,
                                boolean authenticate)
                         throws TwitterException
Description copied from interface: Twitter.IHttpClient
Send an HTTP GET request and return the response body. Note that this will change all line breaks into system line breaks!

Specified by:
getPage in interface Twitter.IHttpClient
Parameters:
uri - The uri to fetch
vars - get arguments to add to the uri
authenticate - If true, use authentication. The authentication method used depends on the implementation (basic-auth, OAuth). It is an error to use true if no authentication details have been set.
Throws:
TwitterException - for a variety of reasons
TwitterException.E404 - for resource-does-not-exist errors

getRateLimit

public RateLimit getRateLimit(Twitter.KRequestType reqType)
Specified by:
getRateLimit in interface Twitter.IHttpClient
See Also:
This is where the Twitter method is implemented.

getRequestToken

public Token getRequestToken()
Returns:
the request token, if one has been created via authorizeUrl().

post

public java.lang.String post(java.lang.String uri,
                             java.util.Map<java.lang.String,java.lang.String> vars,
                             boolean authenticate)
                      throws TwitterException
Description copied from interface: Twitter.IHttpClient
Send an HTTP POST request and return the response body.

Specified by:
post in interface Twitter.IHttpClient
Parameters:
uri - The uri to post to.
vars - The form variables to send. These are URL encoded before sending.
authenticate - If true, send user authentication
Returns:
The response from the server.
Throws:
TwitterException - for a variety of reasons
TwitterException.E404 - for resource-does-not-exist errors

post2_connect

public java.net.HttpURLConnection post2_connect(java.lang.String uri,
                                                java.util.Map<java.lang.String,java.lang.String> vars)
                                         throws TwitterException
Description copied from interface: Twitter.IHttpClient
Lower-level POST method.

Specified by:
post2_connect in interface Twitter.IHttpClient
Returns:
a freshly opened authorised connection
Throws:
TwitterException

setAuthorizationCode

public void setAuthorizationCode(java.lang.String verifier)
                          throws java.lang.RuntimeException
Set the authorisation code (aka the verifier). This is only relevant when using out-of-band instead of a callback-url.

Parameters:
verifier - a pin code which Twitter gives the user
Throws:
java.lang.RuntimeException - Scribe throws an exception if the verifier is invalid

setRetryOnError

public void setRetryOnError(boolean retryOnError)
False by default. Setting this to true switches on a robustness workaround: when presented with a 50X server error, the system will wait 1 second and make a second attempt. This is NOT thread safe.


setTimeout

@Deprecated
public void setTimeout(int millisecs)
Deprecated. 

This does not do anything at present!

Specified by:
setTimeout in interface Twitter.IHttpClient

updateRateLimits

public void updateRateLimits(Twitter.KRequestType reqType)