代码拉取完成,页面将自动刷新
同步操作将从 githubsync/vertx-examples 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package io.vertx.example.webclient.oauth;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.example.util.Runner;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.codec.BodyCodec;
/*
* @author <a href="mailto:akshay0007k@gmail.com">Akshay Kumar</a>
*/
public class TwitterOAuthExample extends AbstractVerticle {
// 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());
}
});
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。