Skip to content

Commit

Permalink
Fix example to be polyglot
Browse files Browse the repository at this point in the history
  • Loading branch information
pmlopes committed Jul 14, 2017
1 parent 0204910 commit 352d995
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 58 deletions.
@@ -0,0 +1,32 @@
import groovy.transform.Field
import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.codec.BodyCodec
@Field def AUTH_URL = "https://api.twitter.com/oauth2/token"
@Field def TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json"
@Field def B64_ENCODED_AUTH = "base64(your-consumer-key:your-consumer-secret)"

// Create the web client.
def client = WebClient.create(vertx)

def queryToSearch = "vertx"

// First we need to authenticate our call.
def authHeader = "Basic ${B64_ENCODED_AUTH}"
client.postAbs(AUTH_URL).as(BodyCodec.jsonObject()).addQueryParam("grant_type", "client_credentials").putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8").putHeader("Authorization", authHeader).send({ authHandler ->
// Authentication successful.
if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) {
def authJson = authHandler.result().body()
def accessToken = authJson.access_token
def header = "Bearer ${accessToken}"
// Making call to search tweets.
client.getAbs(TWEET_SEARCH_URL).as(BodyCodec.jsonObject()).addQueryParam("q", queryToSearch).putHeader("Authorization", header).send({ handler ->
if (handler.succeeded() && 200 == handler.result().statusCode()) {
println(handler.result().body())
} else {
println(handler.cause().getMessage())
}
})
} else {
println(authHandler.cause().getMessage())
}
})
Expand Up @@ -6,67 +6,57 @@
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.codec.BodyCodec;

import java.util.Base64;

/*
* @author <a href="mailto:akshay0007k@gmail.com">Akshay Kumar</a>
*/
public class TwitterOAuthExample extends AbstractVerticle {

// Provided by twitter after registering your app.
private static final String CONSUMER_KEY = "your-consumer-key";
private static final String CONSUMER_SECRET = "your-consumer-secret";

private static final String AUTH_URL = "https://api.twitter.com/oauth2/token";
private static final String TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json";

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(TwitterOAuthExample.class);
}

@Override
public void start() throws Exception {

// Create the web client.
WebClient client = WebClient.create(vertx);

String queryToSearch = "vertx";

// First we need to authenticate our call.
String encodedAuth = getEncodedAuth();
String authHeader = "Basic " + encodedAuth;
client.postAbs(AUTH_URL)
.as(BodyCodec.jsonObject())
.addQueryParam("grant_type", "client_credentials")
.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.putHeader("Authorization", authHeader)
.send(authHandler -> {
// Authentication successful.
if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) {
JsonObject authJson = authHandler.result().body();
String accessToken = authJson.getString("access_token");
String header = "Bearer " + accessToken;
// Making call to search tweets.
client.getAbs(TWEET_SEARCH_URL)
.as(BodyCodec.jsonObject())
.addQueryParam("q", queryToSearch)
.putHeader("Authorization", header)
.send(handler -> {
if (handler.succeeded() && 200 == handler.result().statusCode()) {
System.out.println(handler.result().body());
} else {
System.out.println(handler.cause().getMessage());
}
});
} else { // Authentication failed
System.out.println(authHandler.cause().getMessage());
}
});
}

private static String getEncodedAuth() {
String key = CONSUMER_KEY + ":" + CONSUMER_SECRET;
return Base64.getEncoder().encodeToString(key.getBytes());
}
// consumer key and secret are provided by twitter after registering your app.
private static final String B64_ENCODED_AUTH = "base64(your-consumer-key:your-consumer-secret)";
private static final String AUTH_URL = "https://api.twitter.com/oauth2/token";
private static final String TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json";

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(TwitterOAuthExample.class);
}

@Override
public void start() throws Exception {

// Create the web client.
WebClient client = WebClient.create(vertx);

String queryToSearch = "vertx";

// First we need to authenticate our call.
String authHeader = "Basic " + B64_ENCODED_AUTH;
client.postAbs(AUTH_URL)
.as(BodyCodec.jsonObject())
.addQueryParam("grant_type", "client_credentials")
.putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.putHeader("Authorization", authHeader)
.send(authHandler -> {
// Authentication successful.
if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) {
JsonObject authJson = authHandler.result().body();
String accessToken = authJson.getString("access_token");
String header = "Bearer " + accessToken;
// Making call to search tweets.
client.getAbs(TWEET_SEARCH_URL)
.as(BodyCodec.jsonObject())
.addQueryParam("q", queryToSearch)
.putHeader("Authorization", header)
.send(handler -> {
if (handler.succeeded() && 200 == handler.result().statusCode()) {
System.out.println(handler.result().body());
} else {
System.out.println(handler.cause().getMessage());
}
});
} else { // Authentication failed
System.out.println(authHandler.cause().getMessage());
}
});
}
}
@@ -0,0 +1,31 @@
var WebClient = require("vertx-web-client-js/web_client");
var BodyCodec = require("vertx-web-common-js/body_codec");
var AUTH_URL = "https://api.twitter.com/oauth2/token";
var TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json";
var B64_ENCODED_AUTH = "base64(your-consumer-key:your-consumer-secret)";

// Create the web client.
var client = WebClient.create(vertx);

var queryToSearch = "vertx";

// First we need to authenticate our call.
var authHeader = "Basic " + B64_ENCODED_AUTH;
client.postAbs(AUTH_URL).as(BodyCodec.jsonObject()).addQueryParam("grant_type", "client_credentials").putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8").putHeader("Authorization", authHeader).send(function (authHandler, authHandler_err) {
// Authentication successful.
if (authHandler_err == null && 200 === authHandler.statusCode()) {
var authJson = authHandler.body();
var accessToken = authJson.access_token;
var header = "Bearer " + accessToken;
// Making call to search tweets.
client.getAbs(TWEET_SEARCH_URL).as(BodyCodec.jsonObject()).addQueryParam("q", queryToSearch).putHeader("Authorization", header).send(function (handler, handler_err) {
if (handler_err == null && 200 === handler.statusCode()) {
console.log(handler.body());
} else {
console.log(handler_err.getMessage());
}
});
} else {
console.log(authHandler_err.getMessage());
}
});
@@ -0,0 +1,38 @@
package io.vertx.example.webclient.oauth

import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.codec.BodyCodec

class TwitterOAuthExample : io.vertx.core.AbstractVerticle() {
var AUTH_URL = "https://api.twitter.com/oauth2/token"
var TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json"
var B64_ENCODED_AUTH = "base64(your-consumer-key:your-consumer-secret)"
override fun start() {

// Create the web client.
var client = WebClient.create(vertx)

var queryToSearch = "vertx"

// First we need to authenticate our call.
var authHeader = "Basic ${B64_ENCODED_AUTH}"
client.postAbs(AUTH_URL).as(BodyCodec.jsonObject()).addQueryParam("grant_type", "client_credentials").putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8").putHeader("Authorization", authHeader).send({ authHandler ->
// Authentication successful.
if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) {
var authJson = authHandler.result().body()
var accessToken = authJson.getString("access_token")
var header = "Bearer ${accessToken}"
// Making call to search tweets.
client.getAbs(TWEET_SEARCH_URL).as(BodyCodec.jsonObject()).addQueryParam("q", queryToSearch).putHeader("Authorization", header).send({ handler ->
if (handler.succeeded() && 200 == handler.result().statusCode()) {
println(handler.result().body())
} else {
println(handler.cause().getMessage())
}
})
} else {
println(authHandler.cause().getMessage())
}
})
}
}
@@ -0,0 +1,31 @@
require 'vertx-web-client/web_client'
require 'vertx-web-common/body_codec'
@AUTH_URL = "https://api.twitter.com/oauth2/token"
@TWEET_SEARCH_URL = "https://api.twitter.com/1.1/search/tweets.json"
@B64_ENCODED_AUTH = "base64(your-consumer-key:your-consumer-secret)"

# Create the web client.
client = VertxWebClient::WebClient.create($vertx)

queryToSearch = "vertx"

# First we need to authenticate our call.
authHeader = "Basic #{@B64_ENCODED_AUTH}"
client.post_abs(@AUTH_URL).as(VertxWebCommon::BodyCodec.json_object()).add_query_param("grant_type", "client_credentials").put_header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8").put_header("Authorization", authHeader).send() { |authHandler_err,authHandler|
# Authentication successful.
if (authHandler_err == nil && 200 == authHandler.status_code())
authJson = authHandler.body()
accessToken = authJson['access_token']
header = "Bearer #{accessToken}"
# Making call to search tweets.
client.get_abs(@TWEET_SEARCH_URL).as(VertxWebCommon::BodyCodec.json_object()).add_query_param("q", queryToSearch).put_header("Authorization", header).send() { |handler_err,handler|
if (handler_err == nil && 200 == handler.status_code())
puts handler.body()
else
puts handler_err.get_message()
end
}
else
puts authHandler_err.get_message()
end
}

0 comments on commit 352d995

Please sign in to comment.