1 /** 2 * Copyright (c) 2012-2015, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.odesk; 31 32 import com.jcabi.log.Logger; 33 import java.util.Scanner; 34 import org.hamcrest.Matchers; 35 import org.junit.Assume; 36 import org.junit.Test; 37 import org.scribe.builder.ServiceBuilder; 38 import org.scribe.model.Token; 39 import org.scribe.model.Verifier; 40 import org.scribe.oauth.OAuthService; 41 42 /** 43 * OAuth example for {@link RtOdesk}. 44 * 45 * <p>Run this example from command line like this: 46 * 47 * <pre> 48 * $ mvn clean install -Dit.test=RtOdeskOauthExample \ 49 * -Dfailsafe.odesk.key=... -Dfailsafe.odesk.secret=... 50 * </pre> 51 * 52 * @author Yegor Bugayenko (yegor@tpc2.com) 53 * @version $Id: ce5ed62b27bbfd98d1e866af83ca4ef8f6966deb $ 54 * @checkstyle ClassDataAbstractionCoupling (500 lines) 55 */ 56 public final class RtOdeskOauthExample { 57 58 /** 59 * Odesk key. 60 */ 61 private static final String KEY = 62 System.getProperty("failsafe.odesk.key"); 63 64 /** 65 * Odesk secret. 66 */ 67 private static final String SECRET = 68 System.getProperty("failsafe.odesk.secret"); 69 70 /** 71 * Odesk access token can be obtained through OAuth. 72 * @throws Exception If some problem inside 73 */ 74 @Test 75 public void obtainsAccessToken() throws Exception { 76 Assume.assumeThat(RtOdeskOauthExample.KEY, Matchers.notNullValue()); 77 final OAuthService service = new ServiceBuilder() 78 .provider(OAuthWire.OdeskApi.class) 79 .apiKey(RtOdeskOauthExample.KEY) 80 .apiSecret(RtOdeskOauthExample.SECRET) 81 .build(); 82 final Token rqst = service.getRequestToken(); 83 Logger.info( 84 this, "authorization URL: %s (open it in a browser)", 85 service.getAuthorizationUrl(rqst) 86 ); 87 Logger.info(this, "enter Odesk verifier and press ENTER:"); 88 final Scanner input = new Scanner(System.in); 89 final Verifier verifier = new Verifier(input.nextLine()); 90 final Token access = service.getAccessToken(rqst, verifier); 91 Logger.info(this, "access token is: %s", access.getToken()); 92 Logger.info(this, "access token secret is: %s", access.getSecret()); 93 } 94 95 }