Search in sources :

Example 6 with Response

use of com.github.scribejava.core.model.Response in project scribejava by scribejava.

the class NeteaseWeiboExample 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(NeteaseWeibooApi.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)

Example 7 with Response

use of com.github.scribejava.core.model.Response in project scribejava by scribejava.

the class PinterestExample 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_app_id";
    final String apiSecret = "your_app_secret";
    final OAuth20Service service = new ServiceBuilder().apiKey(apiKey).apiSecret(apiSecret).scope("read_public,write_public,read_relationships,write_relationships").callback(// Add as valid callback in developer portal
    "https://localhost:9000/").build(PinterestApi.instance());
    final Scanner in = new Scanner(System.in);
    System.out.println("=== Pinterest's OAuth Workflow ===");
    System.out.println();
    // Obtain the Authorization URL
    System.out.println("Fetching the Authorization URL...");
    final String authorizationUrl = service.getAuthorizationUrl();
    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 code = 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 OAuth2AccessToken accessToken = service.getAccessToken(code);
    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 + accessToken.getAccessToken());
    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) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 8 with Response

use of com.github.scribejava.core.model.Response in project scribejava by scribejava.

the class Px500Example 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(Px500Api.instance());
    final Scanner in = new Scanner(System.in);
    System.out.println("=== 500Px'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.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 9 with Response

use of com.github.scribejava.core.model.Response in project scribejava by scribejava.

the class RenrenExample 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 api key";
    final String apiSecret = "your api secret";
    final OAuth20Service service = new ServiceBuilder().apiKey(apiKey).apiSecret(apiSecret).scope("status_update publish_feed").callback("http://your.doman.com/oauth/renren").build(RenrenApi.instance());
    final Scanner in = new Scanner(System.in);
    System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
    System.out.println();
    // Obtain the Authorization URL
    System.out.println("Fetching the Authorization URL...");
    final String authorizationUrl = service.getAuthorizationUrl();
    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 code = 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 OAuth2AccessToken accessToken = service.getAccessToken(code);
    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.POST, PROTECTED_RESOURCE_URL);
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("method", "users.getInfo");
    parameters.put("format", "json");
    parameters.put("v", "1.0");
    final List<String> sigString = new ArrayList<>(parameters.size() + 1);
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        request.addQuerystringParameter(entry.getKey(), entry.getValue());
        sigString.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
    }
    sigString.add(String.format("%s=%s", OAuthConstants.ACCESS_TOKEN, accessToken.getAccessToken()));
    Collections.sort(sigString);
    final StringBuilder b = new StringBuilder();
    for (String param : sigString) {
        b.append(param);
    }
    b.append(apiSecret);
    System.out.println("Sig string: " + b.toString());
    request.addQuerystringParameter("sig", md5(b.toString()));
    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) Scanner(java.util.Scanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) Response(com.github.scribejava.core.model.Response) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with Response

use of com.github.scribejava.core.model.Response in project scribejava by scribejava.

the class JDKHttpClient method doExecute.

private Response doExecute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, BodyType bodyType, Object bodyContents) throws IOException {
    final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection();
    connection.setInstanceFollowRedirects(config.isFollowRedirects());
    connection.setRequestMethod(httpVerb.name());
    if (config.getConnectTimeout() != null) {
        connection.setConnectTimeout(config.getConnectTimeout());
    }
    if (config.getReadTimeout() != null) {
        connection.setReadTimeout(config.getReadTimeout());
    }
    addHeaders(connection, headers, userAgent);
    if (httpVerb.isPermitBody()) {
        bodyType.setBody(connection, bodyContents);
    }
    try {
        connection.connect();
        final int responseCode = connection.getResponseCode();
        return new Response(responseCode, connection.getResponseMessage(), parseHeaders(connection), responseCode >= 200 && responseCode < 400 ? connection.getInputStream() : connection.getErrorStream());
    } catch (UnknownHostException e) {
        throw new OAuthException("The IP address of a host could not be determined.", e);
    }
}
Also used : Response(com.github.scribejava.core.model.Response) HttpURLConnection(java.net.HttpURLConnection) UnknownHostException(java.net.UnknownHostException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) URL(java.net.URL)

Aggregations

Response (com.github.scribejava.core.model.Response)61 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)56 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)49 Scanner (java.util.Scanner)49 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)29 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)19 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)19 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)18 Random (java.util.Random)11 HashMap (java.util.HashMap)7 OAuthConfig (com.github.scribejava.core.model.OAuthConfig)3 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)3 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)3 HttpUrl (okhttp3.HttpUrl)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Test (org.junit.Test)3 SalesforceToken (com.github.scribejava.apis.salesforce.SalesforceToken)2