Search in sources :

Example 16 with ServiceBuilder

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());
}
Also used : OAuth2Authorization(com.github.scribejava.core.model.OAuth2Authorization) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) Test(org.junit.Test)

Example 17 with ServiceBuilder

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"));
}
Also used : OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Gson(com.google.gson.Gson) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) Test(org.junit.Test)

Example 18 with ServiceBuilder

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"));
}
Also used : OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Gson(com.google.gson.Gson) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) Test(org.junit.Test)

Example 19 with ServiceBuilder

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! :)");
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth1AccessToken(com.github.scribejava.core.model.OAuth1AccessToken) OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 20 with ServiceBuilder

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! :)");
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth1AccessToken(com.github.scribejava.core.model.OAuth1AccessToken) OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Aggregations

ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)52 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)49 Response (com.github.scribejava.core.model.Response)49 Scanner (java.util.Scanner)49 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)31 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)18 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)18 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)18 Random (java.util.Random)11 HashMap (java.util.HashMap)4 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)3 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)3 Test (org.junit.Test)3 SalesforceToken (com.github.scribejava.apis.salesforce.SalesforceToken)2 Gson (com.google.gson.Gson)2 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)2 LinkedInApi (com.github.scribejava.apis.LinkedInApi)1 HttpClientConfig (com.github.scribejava.core.httpclient.HttpClientConfig)1 OAuth2Authorization (com.github.scribejava.core.model.OAuth2Authorization)1