public static void main(String[] args) throws IOException, KeyManagementException, NoSuchAlgorithmException {
int timeint = (int) (System.currentTimeMillis() / 1000);
String oauth_timestamp = Integer.toString(timeint);
int nonce = (int) (Math.random() * 100000000);
String oauth_nonce = Integer.toString(nonce);
String url = "https://api.twitter.com/1.1/account_activity/all/{env_name}/webhooks.json?url=https://{my_domain}/webhook/twitter";
String oauth_consumer_key ={...};
String oauth_token = {...};
String oauth_consumer_secret = {...};
String oauth_token_secret = {...};
String params = "include_entities=true&oauth_consumer_key=" + oauth_consumer_key + "&oauth_nonce=" + oauth_nonce
+ "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + oauth_token
+ "&oauth_version=1.0";
String signature_base_string = "POST" + "&" + encodeURIComponent(url) + "&" + encodeURIComponent(params);
String oauth_signature = generateSignature(signature_base_string, oauth_consumer_secret, oauth_token_secret);
oauth_signature = encodeURIComponent(oauth_signature);
System.out.println("oauth_signature:" + oauth_signature);
URL obj = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
conn.setRequestMethod("POST");
makeIgnoreCertificate(conn);
//conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Authorization",
"OAuth oauth_consumer_key=\"" + oauth_consumer_key + "\", oauth_nonce=\"" + oauth_nonce
+ "\", oauth_signature=\"" + oauth_signature
+ "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + oauth_timestamp
+ "\", oauth_token=\"" + oauth_token + "\", oauth_version=\"1.0\"");
// POST output
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(params.getBytes());
os.flush();
os.close();
// response code
int responseCode = conn.getResponseCode();
System.out.println("Twitter POST Response Code :: " + responseCode);
// error stream
InputStream errorstream = conn.getErrorStream();
BufferedReader br = null;
if (errorstream == null) {
InputStream inputstream = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(inputstream));
} else {
br = new BufferedReader(new InputStreamReader(errorstream));
}
String response = "";
String nachricht;
while ((nachricht = br.readLine()) != null) {
response += nachricht;
}
System.out.println("error stream :: " + response);
}
public static String generateSignature(String signatureBaseStr, String oAuthConsumerSecret,
String oAuthTokenSecret) {
String hash = null;
try {
Mac sha1_HMAC = Mac.getInstance("HmacSHA1");
SecretKeySpec secret_key;
String signingKey = encodeURIComponent(oAuthConsumerSecret) + '&' + encodeURIComponent(oAuthTokenSecret);
secret_key = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
sha1_HMAC.init(secret_key);
hash = Base64.encodeBase64String(sha1_HMAC.doFinal(signatureBaseStr.getBytes()));
} catch (Exception e) {
}
return hash;
}
public static String encodeURIComponent(String s) {
String result;
try {
result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\&", "%26");
} catch (UnsupportedEncodingException e) {
result = s;
}
return result;
}
This is my code to try to register a webhook to AAAPI and I do have a premium/sandbox account. I can use the same code, only by changing the url to “https://api.twitter.com/1.1/statuses/update.json” and add status string to update a tweet successfully, which means all my signature and tokens and secrets should be correct, right? Can somebody help me out please? thanks!