use of com.github.scribejava.core.builder.ServiceBuilder in project scribejava by scribejava.
the class OAuth20ServiceTest method testOAuthExtractAuthorization.
@Test
public void testOAuthExtractAuthorization() {
final OAuth20Service service = new ServiceBuilder().apiKey("your_api_key").apiSecret("your_api_secret").build(new OAuth20ApiUnit());
OAuth2Authorization authorization = service.extractAuthorization("https://cl.ex.com/cb?code=SplxlOB&state=xyz");
assertEquals("SplxlOB", authorization.getCode());
assertEquals("xyz", authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?state=xyz&code=SplxlOB");
assertEquals("SplxlOB", authorization.getCode());
assertEquals("xyz", authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?key=value&state=xyz&code=SplxlOB");
assertEquals("SplxlOB", authorization.getCode());
assertEquals("xyz", authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?state=xyz&code=SplxlOB&key=value&");
assertEquals("SplxlOB", authorization.getCode());
assertEquals("xyz", authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?code=SplxlOB&state=");
assertEquals("SplxlOB", authorization.getCode());
assertEquals(null, authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?code=SplxlOB");
assertEquals("SplxlOB", authorization.getCode());
assertEquals(null, authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?code=");
assertEquals(null, authorization.getCode());
assertEquals(null, authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?code");
assertEquals(null, authorization.getCode());
assertEquals(null, authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb?");
assertEquals(null, authorization.getCode());
assertEquals(null, authorization.getState());
authorization = service.extractAuthorization("https://cl.ex.com/cb");
assertEquals(null, authorization.getCode());
assertEquals(null, authorization.getState());
}
use of com.github.scribejava.core.builder.ServiceBuilder in project scribejava by scribejava.
the class OAuth20ServiceTest method shouldProduceCorrectRequestSync.
@Test
public void shouldProduceCorrectRequestSync() throws IOException, InterruptedException, ExecutionException {
final OAuth20Service service = new ServiceBuilder().apiKey("your_api_key").apiSecret("your_api_secret").build(new OAuth20ApiUnit());
final OAuth2AccessToken token = service.getAccessTokenPasswordGrant("user1", "password1");
final Gson json = new Gson();
assertNotNull(token);
final Map<String, String> map = json.fromJson(token.getRawResponse(), new TypeTokenImpl().getType());
assertEquals(OAuth20ServiceUnit.TOKEN, map.get(OAuthConstants.ACCESS_TOKEN));
assertEquals(OAuth20ServiceUnit.STATE, map.get(OAuthConstants.STATE));
assertEquals(OAuth20ServiceUnit.EXPIRES, map.get("expires_in"));
final String authorize = Base64Encoder.getInstance().encode(String.format("%s:%s", service.getConfig().getApiKey(), service.getConfig().getApiSecret()).getBytes(Charset.forName("UTF-8")));
assertEquals(OAuthConstants.BASIC + " " + authorize, map.get(OAuthConstants.HEADER));
assertEquals("user1", map.get("query-username"));
assertEquals("password1", map.get("query-password"));
assertEquals("password", map.get("query-grant_type"));
}
use of com.github.scribejava.core.builder.ServiceBuilder in project scribejava by scribejava.
the class OAuth20ServiceTest method shouldProduceCorrectRequestAsync.
@Test
public void shouldProduceCorrectRequestAsync() throws ExecutionException, InterruptedException {
final OAuth20Service service = new ServiceBuilder().apiKey("your_api_key").apiSecret("your_api_secret").build(new OAuth20ApiUnit());
final OAuth2AccessToken token = service.getAccessTokenPasswordGrantAsync("user1", "password1", null).get();
final Gson json = new Gson();
assertNotNull(token);
final Map<String, String> map = json.fromJson(token.getRawResponse(), new TypeTokenImpl().getType());
assertEquals(OAuth20ServiceUnit.TOKEN, map.get(OAuthConstants.ACCESS_TOKEN));
assertEquals(OAuth20ServiceUnit.STATE, map.get(OAuthConstants.STATE));
assertEquals(OAuth20ServiceUnit.EXPIRES, map.get("expires_in"));
final String authorize = Base64Encoder.getInstance().encode(String.format("%s:%s", service.getConfig().getApiKey(), service.getConfig().getApiSecret()).getBytes(Charset.forName("UTF-8")));
assertEquals(OAuthConstants.BASIC + " " + authorize, map.get(OAuthConstants.HEADER));
assertEquals("user1", map.get("query-username"));
assertEquals("password1", map.get("query-password"));
assertEquals("password", map.get("query-grant_type"));
}
use of com.github.scribejava.core.builder.ServiceBuilder in project scribejava by scribejava.
the class SkyrockExample method main.
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
final OAuth10aService service = new ServiceBuilder().apiKey("your-api-key").apiSecret("your-api-secret").build(SkyrockApi.instance());
final Scanner in = new Scanner(System.in);
System.out.println("=== Skyrock's OAuth Workflow ===");
System.out.println();
// Obtain the Request Token
System.out.println("Fetching the Request Token...");
final OAuth1RequestToken requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();
System.out.println("Now go and authorize ScribeJava here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
final String oauthVerifier = in.nextLine();
System.out.println();
// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
System.out.println();
// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
final Response response = service.execute(request);
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());
System.out.println();
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
}
use of com.github.scribejava.core.builder.ServiceBuilder in project scribejava by scribejava.
the class SohuWeiboExample method main.
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
// Replace these with your own api key and secret
final String apiKey = "your_key";
final String apiSecret = "your_secret";
final OAuth10aService service = new ServiceBuilder().apiKey(apiKey).apiSecret(apiSecret).build(SohuWeiboApi.instance());
final Scanner in = new Scanner(System.in);
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();
// Grab a request token.
System.out.println("Fetching request token.");
final OAuth1RequestToken requestToken = service.getRequestToken();
System.out.println("Got it ... ");
System.out.println(requestToken.getToken());
// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
final String authorizationUrl = service.getAuthorizationUrl(requestToken);
System.out.println("Got the Authorization URL!");
System.out.println("Now go and authorize ScribeJava here:");
System.out.println(authorizationUrl);
System.out.println("And paste the authorization code here");
System.out.print(">>");
final String oauthVerifier = in.nextLine();
System.out.println();
// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
System.out.println();
// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
final Response response = service.execute(request);
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());
System.out.println();
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
}
Aggregations