Search in sources :

Example 1 with OAuthProvider

use of oauth.signpost.OAuthProvider 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 2 with OAuthProvider

use of oauth.signpost.OAuthProvider 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 3 with OAuthProvider

use of oauth.signpost.OAuthProvider in project mobile-android by photo.

the class Preferences method getOAuthProvider.

public static OAuthProvider getOAuthProvider(Context context) {
    String serverUrl = getServer(context);
    OAuthProvider provider = new DefaultOAuthProvider(serverUrl + "/v1/oauth/token/request", serverUrl + "/v1/oauth/token/access", serverUrl + "/v1/oauth/authorize");
    provider.setOAuth10a(true);
    return provider;
}
Also used : DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthProvider(oauth.signpost.OAuthProvider) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider)

Example 4 with OAuthProvider

use of oauth.signpost.OAuthProvider in project data-transfer-project by google.

the class SmugMugAuth method generateAuthData.

@Override
public AuthData generateAuthData(IOInterface ioInterface) throws IOException {
    // As per details: https://api.smugmug.com/api/v2/doc/tutorial/authorization.html
    // and example:
    // http://stackoverflow.com/questions/15194182/examples-for-oauth1-using-google-api-java-oauth
    // Google library puts signature in header and not in request, see https://oauth.net/1/
    OAuthConsumer consumer = new GoogleOAuthConsumer(appCredentials.key(), appCredentials.secret());
    String permissions = (serviceMode == ServiceMode.EXPORT) ? "Read" : "Add";
    OAuthProvider provider = new DefaultOAuthProvider("https://secure.smugmug.com/services/oauth/1.0a/getRequestToken", "https://secure.smugmug.com/services/oauth/1.0a/getAccessToken", "https://secure.smugmug.com/services/oauth/1.0a/authorize?Access=Full&Permissions=" + permissions);
    String authUrl;
    try {
        authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
    } catch (OAuthMessageSignerException | OAuthNotAuthorizedException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't generate authUrl", e);
    }
    String code = ioInterface.ask("Please visit: " + authUrl + " and enter code:");
    try {
        provider.retrieveAccessToken(consumer, code.trim());
    } catch (OAuthMessageSignerException | OAuthNotAuthorizedException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't authorize", e);
    }
    return TokenSecretAuthData.create(consumer.getToken(), consumer.getTokenSecret());
}
Also used : OAuthNotAuthorizedException(oauth.signpost.exception.OAuthNotAuthorizedException) GoogleOAuthConsumer(org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthProvider(oauth.signpost.OAuthProvider) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthConsumer(oauth.signpost.OAuthConsumer) GoogleOAuthConsumer(org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer) IOException(java.io.IOException) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 5 with OAuthProvider

use of oauth.signpost.OAuthProvider in project signpost-examples by mttkay.

the class FireEagleMain method main.

public static void main(String[] args) throws Exception {
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer("2qnk0OzpuzzU", "Ctp1QtFbtSaFhOJbOLMCUPio9c75zIaG");
    OAuthProvider provider = new CommonsHttpOAuthProvider("https://fireeagle.yahooapis.com/oauth/request_token", "https://fireeagle.yahooapis.com/oauth/access_token", "https://fireeagle.yahoo.net/oauth/authorize");
    System.out.println("Fetching request token from Fire Eagle...");
    // we do not support callbacks, thus pass OOB
    String authUrl = provider.retrieveRequestToken(consumer, "http://www.example.com");
    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 verification code and hit ENTER when you're done");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String code = br.readLine();
    System.out.println("Fetching access token from Fire Eagle...");
    provider.retrieveAccessToken(consumer, code);
    System.out.println("Access token: " + consumer.getToken());
    System.out.println("Token secret: " + consumer.getTokenSecret());
    HttpPost request = new HttpPost("https://fireeagle.yahooapis.com/api/0.1/update");
    StringEntity body = new StringEntity("city=hamburg&label=" + URLEncoder.encode("Send via Signpost!", "UTF-8"));
    body.setContentType("application/x-www-form-urlencoded");
    request.setEntity(body);
    consumer.sign(request);
    System.out.println("Sending update request to Fire Eagle...");
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);
    System.out.println("Response: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CommonsHttpOAuthConsumer(oauth.signpost.commonshttp.CommonsHttpOAuthConsumer) CommonsHttpOAuthProvider(oauth.signpost.commonshttp.CommonsHttpOAuthProvider) InputStreamReader(java.io.InputStreamReader) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) HttpResponse(org.apache.http.HttpResponse) CommonsHttpOAuthProvider(oauth.signpost.commonshttp.CommonsHttpOAuthProvider) OAuthProvider(oauth.signpost.OAuthProvider) OAuthConsumer(oauth.signpost.OAuthConsumer) CommonsHttpOAuthConsumer(oauth.signpost.commonshttp.CommonsHttpOAuthConsumer) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

OAuthProvider (oauth.signpost.OAuthProvider)7 OAuthConsumer (oauth.signpost.OAuthConsumer)6 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 DefaultOAuthConsumer (oauth.signpost.basic.DefaultOAuthConsumer)3 IOException (java.io.IOException)2 Credentials (com.google.refine.oauth.Credentials)1 Provider (com.google.refine.oauth.Provider)1 URISyntaxException (java.net.URISyntaxException)1 ServletException (javax.servlet.ServletException)1 CommonsHttpOAuthConsumer (oauth.signpost.commonshttp.CommonsHttpOAuthConsumer)1 CommonsHttpOAuthProvider (oauth.signpost.commonshttp.CommonsHttpOAuthProvider)1 OAuthCommunicationException (oauth.signpost.exception.OAuthCommunicationException)1 OAuthExpectationFailedException (oauth.signpost.exception.OAuthExpectationFailedException)1 OAuthMessageSignerException (oauth.signpost.exception.OAuthMessageSignerException)1 OAuthNotAuthorizedException (oauth.signpost.exception.OAuthNotAuthorizedException)1 HttpResponse (org.apache.http.HttpResponse)1