Search in sources :

Example 1 with OAuthConsumer

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());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) BufferedReader(java.io.BufferedReader) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthProvider(oauth.signpost.OAuthProvider) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) OAuthConsumer(oauth.signpost.OAuthConsumer) URL(java.net.URL)

Example 2 with OAuthConsumer

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();
}
Also used : OAuthException(oauth.signpost.exception.OAuthException) OAuthConsumer(oauth.signpost.OAuthConsumer)

Example 3 with OAuthConsumer

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);
    }
}
Also used : OAuthProvider(oauth.signpost.OAuthProvider) OAuthConsumer(oauth.signpost.OAuthConsumer) Credentials(com.google.refine.oauth.Credentials) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) OAuthProvider(oauth.signpost.OAuthProvider) Provider(com.google.refine.oauth.Provider)

Example 4 with OAuthConsumer

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;
}
Also used : OAuthConsumer(oauth.signpost.OAuthConsumer)

Example 5 with 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);
    }
}
Also used : DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) CommonsHttpOAuthConsumer(oauth.signpost.commonshttp.CommonsHttpOAuthConsumer) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) OAuthConsumer(oauth.signpost.OAuthConsumer)

Aggregations

OAuthConsumer (oauth.signpost.OAuthConsumer)14 OAuthProvider (oauth.signpost.OAuthProvider)6 DefaultOAuthConsumer (oauth.signpost.basic.DefaultOAuthConsumer)5 DefaultOAuthProvider (oauth.signpost.basic.DefaultOAuthProvider)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 CommonsHttpOAuthConsumer (oauth.signpost.commonshttp.CommonsHttpOAuthConsumer)3 IOException (java.io.IOException)2 OAuthException (oauth.signpost.exception.OAuthException)2 GoogleOAuthConsumer (org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer)2 SharedPreferences (android.content.SharedPreferences)1 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpParameter (com.bluenimble.platform.http.HttpParameter)1 HttpRequestWriteException (com.bluenimble.platform.http.request.HttpRequestWriteException)1 AbstractHttpRequest (com.bluenimble.platform.http.request.impls.AbstractHttpRequest)1 Credentials (com.google.refine.oauth.Credentials)1 Provider (com.google.refine.oauth.Provider)1 URISyntaxException (java.net.URISyntaxException)1