簡體   English   中英

使用Twitter Fabric檢索我自己的帳戶推文

[英]Using Twitter Fabric to retrieve my own account tweets

我目前正在使用Twitter架構框架,並嘗試檢索自己的帳戶推文列表。 我嘗試在線搜索無濟於事。 文檔中的示例顯示了如何顯示基於tweetID的推文。 我不要那個。 我確實知道REST客戶端與提供的變量建立了http連接,並檢索JSON結果以進行解析等。

這是我當前的代碼,成功登錄后將顯示另一個活動,我希望在此顯示我的推文。

          public void success(Result<TwitterSession> result) {
            // Do something with result, which provides a TwitterSession for making API calls
            TwitterSession session = Twitter.getSessionManager().getActiveSession();
            TwitterAuthToken authToken = session.getAuthToken();
            token = authToken.token;
            secret = authToken.secret;
            Log.i("token",token);
            Log.i("secret",secret);
            successNewPage();
        }

我還使用意圖將令牌和密鑰傳遞給了下一個活動

   public void successNewPage(){
    Intent intent = new Intent(this, LoginSuccess.class);
    intent.putExtra("token",token);
    intent.putExtra("secret", secret);
    startActivity(intent);
}

在新的活動課上,我遵循了他們的文檔,並提出了這個建議,

  TwitterAuthConfig authConfig =  new TwitterAuthConfig("consumerKey", "consumerSecret");
    Fabric.with(this, new TwitterCore(authConfig), new TweetUi());

    TwitterCore.getInstance().logInGuest(new Callback() {

        public void success(Result appSessionResult) {

            //Do the rest API HERE
            Bundle extras = getIntent().getExtras();
            String bearerToken = extras.getString("token");
            try {
                fetchTimelineTweet(bearerToken);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        public void failure(TwitterException e) {
            Toast.makeText(getApplicationContext(), "Failure =)",
                    Toast.LENGTH_LONG).show();
        }

    });
}

推文的檢索將是:

 // Fetches the first tweet from a given user's timeline
private static String fetchTimelineTweet(String endPointUrl)
        throws IOException {
    HttpsURLConnection connection = null;



    try {
        URL url = new URL(endPointUrl);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Host", "api.twitter.com");
        connection.setRequestProperty("User-Agent", "anyApplication");
        connection.setRequestProperty("Authorization", "Bearer " +  endPointUrl);
        connection.setUseCaches(false);


        String res = readResponse(connection);
        Log.i("Response", res);
        return new String();
    } catch (MalformedURLException e) {
        throw new IOException("Invalid endpoint URL specified.", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

我在日志中得到的是:

380-380 / com.example.john.fabric W / System.err:java.io.IOException:指定了無效的端點URL。

我的網址錯了嗎? 還是我在endpointURL中設置的令牌也不對嗎? 任何建議將不勝感激。 謝謝!

情況就是這樣,該消息是由fetchTimelineTweet函數引發的。 這應該由以下行引起: URL url = new URL(endPointUrl); 告訴endPointUrl導致MalformedURLException

編輯

根據Twitter開發人員頁面 ,應將其作為endpointURL: https ://dev.twitter.com/rest/reference/get/statuses/user_timeline並傳遞用戶屏幕名稱作為參數。

編輯2

我認為您的代碼應如下所示:

private static String fetchTimelineTweet(String endPointUrl, String token) // this line
        throws IOException {
    HttpsURLConnection connection = null;

    try {
        URL url = new URL(endPointUrl);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Host", "api.twitter.com");
        connection.setRequestProperty("User-Agent", "anyApplication");
        connection.setRequestProperty("Authorization", "Bearer " +  token); // this line
        connection.setUseCaches(false);


        String res = readResponse(connection);
        Log.i("Response", res);
        return new String();
    } catch (MalformedURLException e) {
        throw new IOException("Invalid endpoint URL specified.", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM