Error code 32 when trying to register a webhook


#1
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!


#2

If it is bad authentication (error 32) before it hits the webhook, then you should check to make sure that you have done the following. This information and more is located in our troubleshooting doc:

  1. you are using the proper auth keys that you pulled from the Twitter app that is designated as your AAAPI dev environment. Since this endpoint requires user auth, you will need to use both your consumer keys and access tokens.
  2. you have properly generated the oauth nonce , oauth_signature , and oauth_timestamp for your request.

If you have properly set up your Twitter app, then chances are that you aren’t handling number two properly. If so, please consider using an oauth library (example), one of our github repositories (account-activity-dashboardor twitter-webhook-boilerplate-node), using Insomnia, or try using Twurl.


#3

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.