Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
58 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...t-examples/src/main/groovy/io/vertx/example/webclient/oauth/twitter_o_auth_example.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
web-client-examples/src/main/js/io/vertx/example/webclient/oauth/twitter_o_auth_example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
}); |
38 changes: 38 additions & 0 deletions
38
web-client-examples/src/main/kotlin/io/vertx/example/webclient/oauth/TwitterOAuthExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
}) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
web-client-examples/src/main/ruby/io/vertx/example/webclient/oauth/twitter_o_auth_example.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |