Simplest Java example retrieving user_timeline with twitter API version 1.1

前端 未结 4 891
一向
一向 2021-01-01 02:05

I was looking for a simple Java example using the Twitter 1.1 API and couldn\'t find one. Using the PHP sample posted here: Simplest PHP example for retrieving user_timeline

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 02:46

    You are wrong with the oauth_nonce. It is a random 32 bytes string encoded in base 64.

    You can build them like this :

    public String generateNonce() {
        Random gen = new Random(System.currentTimeMillis());
        StringBuilder nonceBuilder = new StringBuilder("");
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int baseLength = base.length();
    
        // Taking random word characters
        for (int i = 0; i < 32; ++i) {
            int position = gen.nextInt(baseLength);
            nonceBuilder.append(base.charAt(position));
        }
    
        String nonce = toBase64(nonceBuilder.toString());
    
        return nonce;
    }
    
    // In your code :
    String oAuthNonce = generateNonce();
    

    With String toBase64(String); which is a method to encode a String with Base 64.

提交回复
热议问题