Note that there are some explanatory texts on larger screens.

plurals
  1. POTwitter oauth Request Token Response code 401
    primarykey
    data
    text
    <p>I am working on a twitter oauth login. However, when I do the <code>request_token</code>, the very first step, the response code always return <code>401 Unauthorized</code>. </p> <p>I have searched a lot for a week, but I cannot find the solution, please help.</p> <p>Here is my connection:</p> <pre><code>URL url = new URL("https://api.twitter.com/oauth/request_token"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestProperty("Host","api.twitter.com"); conn.setRequestProperty("Authorization", data); conn.setRequestMethod("POST"); conn.connect(); </code></pre> <p>For my data:</p> <pre><code>String data = "OAuth oauth_nonce=\"" + oauth_nonce + "\", oauth_callback=\"" + oauth_callback + "\", oauth_signature_method=\"" + oauth_signature_method + "\", oauth_timestamp=\"" + oauth_timestamp + "\", oauth_consumer_key=\"" + oauth_consumer_key + "\", oauth_signature=\"" + oauth_signature + "\", oauth_version=\"" + oauth_version + "\""; </code></pre> <p>Also, I am sure that my signature is right, because I used the parameter of twitter example, I can calculate the same result as its example, so I think my method is right.</p> <p>Here is my calculation:</p> <pre><code>String oauth_para = "oauth_callback=" + oauth_callback + "&amp;oauth_consumer_key=" + oauth_consumer_key + "&amp;oauth_nonce=" + oauth_nonce + "&amp;oauth_signature_method=" + oauth_signature_method + "&amp;oauth_timestamp=" + oauth_timestamp + "&amp;oauth_version=" + oauth_version; String signingRequests = "POST&amp;" + requestToken + "&amp;" + URLEncoder.encode(oauth_para, "UTF-8"); String key = oauth_consumer_secret + "&amp;"; SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); Mac mac = null; try { mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); } catch(Exception e) { System.err.println("Error: " + e); } byte[] rawHmac = mac.doFinal(signingRequests.getBytes()); String oauth_signature = Base64.encodeBytes(rawHmac); oauth_signature = URLEncoder.encode(oauth_signature); </code></pre> <p>I understand that the nonce and timestamp should be random and unique. So, my method is like that:</p> <pre><code>StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); StringBuffer sb = new StringBuffer(); Random r = new Random(); int range = buffer.length(); for (int i = 0; i &lt; 43;i ++) { sb.append(buffer.charAt(r.nextInt(range))); } long epoch = System.currentTimeMillis() / 1000; String oauth_nonce = sb.toString(); </code></pre> <p>Can somebody help me? </p> <p>P.S: I have also removed my apps, and then create a new one. The result also is the same. Also, the apps is write and read already.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload