use of oauth.signpost.OAuthConsumer in project signpost-examples by mttkay.
the class TwitterMain method main.
public static void main(String[] args) throws Exception {
OAuthConsumer consumer = new DefaultOAuthConsumer("iIlNngv1KdV6XzNYkoLA", "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM", SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize");
System.out.println("Fetching request token from Twitter...");
// we do not support callbacks, thus pass OOB
String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
System.out.println("Request token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization");
System.out.println("Enter the PIN code and hit ENTER when you're done:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pin = br.readLine();
System.out.println("Fetching access token from Twitter...");
provider.retrieveAccessToken(consumer, pin);
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
URL url = new URL("http://twitter.com/statuses/mentions.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
System.out.println("Sending request to Twitter...");
request.connect();
System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage());
}
use of oauth.signpost.OAuthConsumer in project openhab1-addons by openhab.
the class WithingsAuthenticator method finishAuthentication.
/**
* Finishes the OAuth authentication flow.
*
* @param verificationCode
* OAuth verification code
* @param userId
* user id
*/
public synchronized void finishAuthentication(String accountId, String verificationCode, String userId) {
WithingsAccount withingsAccount = getAccount(accountId);
if (withingsAccount == null) {
logger.warn("Couldn't find Credentials of Account '{}'. Please check openhab.cfg or withings.cfg.", accountId);
return;
}
OAuthConsumer consumer = withingsAccount.consumer;
if (provider == null || consumer == null) {
logger.warn("Could not finish authentication. Please execute 'startAuthentication' first.");
return;
}
try {
provider.retrieveAccessToken(consumer, verificationCode);
} catch (OAuthException ex) {
logger.error(ex.getMessage(), ex);
printAuthenticationFailed(ex);
return;
}
withingsAccount.userId = userId;
withingsAccount.setOuathToken(consumer.getToken(), consumer.getTokenSecret());
withingsAccount.registerAccount(componentContext.getBundleContext());
withingsAccount.persist();
printAuthenticationSuccessful();
}
use of oauth.signpost.OAuthConsumer in project OpenRefine by OpenRefine.
the class AuthorizeCommand method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// get the provider from the request
Provider provider = OAuthUtilities.getProvider(request);
try {
// see if the request comes with access credentials
Credentials access_credentials = Credentials.getCredentials(request, provider, Credentials.Type.ACCESS);
// prepare the continuation URL that the OAuth provider will redirect the user to
// (we need to make sure this URL points back to this code or the dance will never complete)
String callbackURL = getBaseURL(request, provider);
if (access_credentials == null) {
// access credentials are not available so we need to check
// to see at what stage of the OAuth dance we are
// get the request token credentials
Credentials request_credentials = Credentials.getCredentials(request, provider, Credentials.Type.REQUEST);
OAuthConsumer consumer = OAuthUtilities.getConsumer(request_credentials, provider);
OAuthProvider pp = provider.getProvider();
if (request_credentials == null) {
// no credentials were found, so let's start the dance
// get the request token
String url = pp.retrieveRequestToken(consumer, callbackURL);
request_credentials = new Credentials(consumer.getToken(), consumer.getTokenSecret(), provider);
// and set them to that we can retrieve them later in the second part of the dance
Credentials.setCredentials(request, response, request_credentials, Credentials.Type.REQUEST, 3600);
// now redirect the user to the Authorize URL where she can authenticate against the
// service provider and authorize us.
// The provider will bounce the user back here for us to continue the dance.
response.sendRedirect(url);
} else {
// we are at the second stage of the dance, so we need need to obtain the access credentials now
// if we got here, it means that the user performed a valid authentication against the
// service provider and authorized us, so now we can request more permanent credentials
// to the service provider and save those as well for later use.
// this is set only for OAuth 1.0a
String verificationCode = request.getParameter(OAUTH_VERIFIER_PARAM);
pp.retrieveAccessToken(consumer, verificationCode);
access_credentials = new Credentials(consumer.getToken(), consumer.getTokenSecret(), provider);
// no matter the result, we need to remove the request token
Credentials.deleteCredentials(request, response, provider, Credentials.Type.REQUEST);
Credentials.setCredentials(request, response, access_credentials, Credentials.Type.ACCESS, 30 * 24 * 3600);
finish(response);
}
} else {
finish(response);
}
} catch (Exception e) {
Credentials.deleteCredentials(request, response, provider, Credentials.Type.REQUEST);
Credentials.deleteCredentials(request, response, provider, Credentials.Type.ACCESS);
respondException(response, e);
}
}
use of oauth.signpost.OAuthConsumer in project OpenRefine by OpenRefine.
the class OAuthUtilities method getConsumer.
public static OAuthConsumer getConsumer(Provider provider) {
if (provider == null) {
throw new RuntimeException("Provider can't be null");
}
String[] consumer_info = infos.get(provider.getHost());
if (consumer_info == null) {
throw new RuntimeException("Can't find secrets for provider '" + provider.getHost() + "'");
}
OAuthConsumer oauthConsumer = provider.createConsumer(consumer_info[0], consumer_info[1]);
oauthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy(provider.getRealm()));
return oauthConsumer;
}
use of oauth.signpost.OAuthConsumer in project androidquery by androidquery.
the class TwitterHandle method applyToken.
@Override
public void applyToken(AbstractAjaxCallback<?, ?> cb, HttpURLConnection conn) {
AQUtility.debug("apply token multipart", cb.getUrl());
OAuthConsumer oac = new DefaultOAuthConsumer(consumer.getConsumerKey(), consumer.getConsumerSecret());
oac.setTokenWithSecret(consumer.getToken(), consumer.getTokenSecret());
try {
oac.sign(conn);
} catch (Exception e) {
AQUtility.report(e);
}
}
Aggregations