From 77d6ad87bd972cd9959b4ca9a12a6496378bb761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Talha=20Mert=20=C4=B0n=C3=B6z=C3=BC?= Date: Sun, 17 Dec 2023 02:44:52 +0300 Subject: [PATCH] added java example --- Manage-Tweets/create_tweet.java | 98 +++++++++++++++++++++++++++++++++ Manage-Tweets/pom.xml | 28 ++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Manage-Tweets/create_tweet.java create mode 100644 Manage-Tweets/pom.xml diff --git a/Manage-Tweets/create_tweet.java b/Manage-Tweets/create_tweet.java new file mode 100644 index 0000000..f09e210 --- /dev/null +++ b/Manage-Tweets/create_tweet.java @@ -0,0 +1,98 @@ +package org.example; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.*; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.security.GeneralSecurityException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.time.Instant; +import java.util.*; + +import com.google.gson.JsonObject; +import oauth.signpost.exception.OAuthCommunicationException; +import oauth.signpost.exception.OAuthExpectationFailedException; +import oauth.signpost.exception.OAuthMessageSignerException; + +public class create_tweet { + public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException, GeneralSecurityException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException { + HttpClient client = HttpClient.newHttpClient(); + + // Create the OAuth parameters + String consumerKey = "CONSUMER_KEY"; // Your consumer key + String consumerSecret = "CONSUMER_SECRET"; // Your consumer secret + String token = "ACCESS_TOKEN"; // Your access token + String tokenSecret = "ACCESS_TOKEN_SECRET"; // Your access token secret + String nonce = randomNonce(); // A random string + String timestamp = String.valueOf(Instant.now().getEpochSecond()); // The current time in seconds since epoch + String signatureMethod = "HMAC-SHA1"; // The signature method + String version = "1.0"; // The OAuth version + String uri = "https://api.twitter.com/2/tweets"; + + JsonObject data = new JsonObject(); + data.addProperty("text", "Your tweet data"); + + String authorizationHeader = generateHeader(uri, consumerKey, consumerSecret, token, tokenSecret, nonce, signatureMethod, timestamp, version); + + // Create an HttpRequest object with the POST method, the URL, the headers and the body + HttpRequest request = HttpRequest.newBuilder() + .POST(HttpRequest.BodyPublishers.ofString(data.toString())) // The body of the request as a query string + .uri(URI.create("https://api.twitter.com/2/tweets")) // The URL of the API endpoint + .header("Content-Type", "application/json") // The content type header + .header("Authorization", authorizationHeader) // The authorization header with the OAuth parameters + .build(); + + // Send the request and get the response + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + // Print the status code and the body of the response + System.out.println(response.statusCode()); + System.out.println(response.body()); + } + + private static String generateHeader(String uri, String consumerKey, String consumerSecret, String token, String tokenSecret, String nonce, String signatureMethod, String timestamp, String version) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { + // Create the signature base string + String signatureBaseString = "POST&" + + URLEncoder.encode(uri, "UTF-8") + "&" + + URLEncoder.encode("oauth_consumer_key=" + consumerKey + "&", "UTF-8") + + URLEncoder.encode("oauth_nonce=" + nonce + "&", "UTF-8") + + URLEncoder.encode("oauth_signature_method=" + signatureMethod + "&", "UTF-8") + + URLEncoder.encode("oauth_timestamp=" + timestamp + "&", "UTF-8") + + URLEncoder.encode("oauth_token=" + token + "&", "UTF-8") + + URLEncoder.encode("oauth_version=" + version, "UTF-8"); + + // Generate the signing key + String signingKey = URLEncoder.encode(consumerSecret, "UTF-8") + "&" + + URLEncoder.encode(tokenSecret, "UTF-8"); + + // Generate the signature using HMAC-SHA1 + SecretKeySpec keySpec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1"); + Mac mac = Mac.getInstance("HmacSHA1"); + mac.init(keySpec); + byte[] result = mac.doFinal(signatureBaseString.getBytes()); + String signature = Base64.getEncoder().encodeToString(result); + + // Create the authorization header + String authorizationHeader = "OAuth " + "oauth_consumer_key=\"" + consumerKey + "\"," + + "oauth_token=\"" + token + "\"," + + "oauth_signature_method=\"" + signatureMethod + "\"," + + "oauth_timestamp=\"" + timestamp + "\"," + + "oauth_nonce=\"" + nonce + "\"," + + "oauth_version=\"" + version + "\"," + + "oauth_signature=\"" + URLEncoder.encode(signature, "UTF-8") + "\""; + + return authorizationHeader; + } + + private static String randomNonce() { + SecureRandom secureRandom = new SecureRandom(); + String randomNonce = String.format("%06d", secureRandom.nextInt(1000000000)); + return randomNonce; + } +} diff --git a/Manage-Tweets/pom.xml b/Manage-Tweets/pom.xml new file mode 100644 index 0000000..d2b370e --- /dev/null +++ b/Manage-Tweets/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + org.example + Twitter API + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + oauth.signpost + signpost-commonshttp4 + 1.2 + + + com.google.code.gson + gson + 2.9.0 + + + \ No newline at end of file