Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- if (args.length < 2) {
- System.err.println("Usage: MakeTopic topicname query");
- System.exit(-1);
- }
- String topicname = args[0];
- String query = args[1];
- // setNoValidation();
- JSONObject authResponse = oauthLogin();
- System.out.println("Login response: " + authResponse.toString(2));
- if (!authResponse.has("access_token")) {
- throw new Exception("OAuth failed: " + authResponse.toString());
- }
- String url = authResponse.getString("instance_url")
- + "/services/data/v23.0/sobjects/PushTopic/";
- JSONObject topic = new JSONObject();
- topic.put("ApiVersion", API_VERSION);
- topic.put("Name", topicname);
- topic.put("Query", query);
- System.out.print("PushTopic data: ");
- System.out.println(topic.toString(2));
- HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(true));
- httpClient.start();
- System.out.println("Push URL : " + url);
- httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/json")
- .header(HttpHeader.AUTHORIZATION, "OAuth " + authResponse.getString("access_token"))
- .content(new StringContentProvider(topic.toString())).send(new Response.CompleteListener() {
- @Override
- public void onComplete(Result result) {
- System.out.println("Result status : " + result.isSucceeded());
- System.out.printf("Response : " + result.getResponse());
- // System.out.println(result.getResponse().getReason());
- // result.getFailure().printStackTrace();
- }
- });
- while (true) {
- TimeUnit.MINUTES.sleep(5);
- }
- }
- private static JSONObject oauthLogin() throws Exception {
- String url = LOGIN_SERVER + "/services/oauth2/token";
- String message = "grant_type=password&client_id=" + CLIENT_ID
- + "&client_secret=" + CLIENT_SECRET + "&username=" + USERNAME
- + "&password=" + PASSWORD;
- HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(true));
- httpClient.start();
- ContentResponse contentResponse = httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded")
- .content(new StringContentProvider(message)).send();
- return new JSONObject(new JSONTokener(contentResponse.getContentAsString()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement