【问题标题】:401 Error Sending GET users/show to Twitter API将 GET 用户/节目发送到 Twitter API 时出现 401 错误
【发布时间】:2014-08-20 11:47:08
【问题描述】:

我试图通过使用 GET users/show 来获取已授权我的应用程序的 twitter 用户的用户详细信息。我为此使用原始 Java,而不是 Twitter API(因此缺少文档)。我能够成功获取请求令牌和访问令牌,但在尝试获取用户信息时的最后一步出现 401 错误。有人注意到我在这里做错了吗?

int t = (int) ((System.currentTimeMillis()) / 1000);
    String n = generateNonce();
    String parameterStr = "oauth_consumer_key=" + TWITTER_KEY +
                          "&oauth_nonce=" + n +
                          "&oauth_signature_method=" + oauthSignatureMethod +
                          "&oauth_timestamp=" + t +
                          "&oauth_token=" + USER_TOKEN +
                          "&oauth_version=1.0" +
                          "&screen_name=" + USER_SCREEN_NAME;
    String oauthSignature = "";
    String authorizationHeaderStr = "";
    try {
        String signatureBaseStr = "GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fusers%2Fshow.json&" + URLEncoder.encode(parameterStr, "UTF-8");
        oauthSignature = URLEncoder.encode(computeSignature(signatureBaseStr, TWITTER_SECRET + "&" + USER_SECRET_TOKEN), "UTF-8");
        authorizationHeaderStr = "OAuth oauth_consumer_key=\"" + TWITTER_KEY +
                                 "\",oauth_nonce=\"" + n +
                                 "\",oauth_signature=\"" + oauthSignature +
                                 "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + t +
                                 "\",oauth_token=\"" + USER_TOKEN +
                                 "\",oauth_version=\"1.0\"";
        System.out.println(signatureBaseStr);
        System.out.println(authorizationHeaderStr);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }

    //Create post and receive oauth token
    try {
        String urlStr = "https://api.twitter.com/1.1/users/show.json?" + "screen_name=" + USER_SCREEN_NAME;
        URL url = new URL(urlStr);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", authorizationHeaderStr);

        //Send post
        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.flush();
        wr.close();

        //Get post
        int twitterResponseCode = conn.getResponseCode();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer twitterResponse = new StringBuffer();

        while((inputLine = in.readLine()) != null) {
            twitterResponse.append(inputLine);
        }
        in.close();
        System.out.println(twitterResponse);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

【问题讨论】:

    标签: twitter twitter-oauth


    【解决方案1】:

    问题是输出流:

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.flush();
        wr.close();
    

    我删除了它,一切正常。老实说,我不确定它为什么会在那里。

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 2021-05-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多