无法在Java api Twitter API调用中授权。错误215

我想使用Java使用twitter的API,但是我无法获得授权。我不能使用twitter4j,因为它不包含我的请求(搜索具有城市名称的地方)。

我已经创建了一个Twitter应用程序开发人员,并且尝试了PostMan的呼叫,它运行良好,因此问题不在于缺少键。

    public void getTweets(City request,StreamObserver<Tweet> responseObserver){
        System.out.print("Inside ipInfo service searching for city: " + request.getcity());

       //Create a 32B nonce code in a private method

        String nonce = generateNonce();
        System.out.print("NONCE = " + nonce + "\n");

        //Create signature

        OAuthConsumer consumer = new OAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
        OAuthaccessToken accessToken = new          OAuthaccessToken(accESS_TOKEN,accESS_TOKEN_KEY);
        SignedRequest threeLeggedOAuthRequest = Signedrequestfactory.create(consumer,accessToken); 

        String Url = "https://api.twitter.com/1.1/geo/search.json";
        HttpMethod method = HttpMethod.GET;
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        long timestamp = ts.getTime();
        String signature = threeLeggedOAuthRequest.getSignature(Url,method,nonce,timestamp);
        System.out.print("Signature = " + signature);

        // Api call

        String placesUrl = String.format("https://api.twitter.com/1.1/geo/search.json?query=%s",request.getcity());

        HttpResponse<JsonNode> apiResponse = null;
        try {
            apiResponse = Unirest.get(placesUrl)
                    .header( "Authorization","OAuth oauth_consumer_key=\""+CONSUMER_KEY+"\"," +
                                    "oauth_token=\""+accESS_TOKEN+"\"," +
                                    "oauth_signature_method=\"HMAC-SHA1\"," +
                                    "oauth_timestamp=\""+timestamp+"\"," +
                                    "oauth_nonce=\""+nonce+"\"," +
                                    "oauth_version=\"1.0\"," +
                                    "oauth_signature=\""+signature+"\""

                    )
                    .header( "accept","*/*" )
                    .header( "Host","api.twitter.com" )
                    .header( "Connection","Keep-Alive" )
                    .asJson();

        } catch (UnirestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.print("Api response = "+ apiResponse.getBody() + "\n");

    }

    private 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 = Base64.getEncoder().encodeToString(nonceBuilder.toString().getBytes());

        return nonce;
    }

结果:Api response = {"errors":[{"code":215,"message":"Bad Authentication data."}]}

liuleihua 回答:无法在Java api Twitter API调用中授权。错误215

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166936.html

大家都在问