2013-03-26 66 views
4

我有與連接到twitter的%&·$ 5#〜€問題。首先,我已經配置了所有(配置文件中的twitter鍵,回調等),然後我調用twitter並打開瀏覽器,然後我在twitter中登錄,然後我在接受應用程序,然後瀏覽器返回到應用程序並嘗試從Twitter獲取響應,但我得到NULL作爲答案。與Twitter連接的Android連接 - 從twitter獲取null答案

任何人都可以幫助我找到這是怎麼回事?

素不相識

PD:我按照這個教程:http://www.androidhive.info/2012/09/android-twitter-oauth-connect-tutorial/

PD 2:有些人認爲,問題是手機(https://dev.twitter.com/discussions/374)的日期&時間,但我改變了這一切,並且不工作

import twitter4j.Twitter; 
import twitter4j.TwitterException; 
import twitter4j.TwitterFactory; 
import twitter4j.User; 
import twitter4j.auth.AccessToken; 
import twitter4j.auth.RequestToken; 
import twitter4j.conf.Configuration; 
import twitter4j.conf.ConfigurationBuilder; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
import android.widget.Toast; 


@SuppressLint("NewApi") 
public class TwitterActivity extends Activity { 

    Intent TWITTER_INTENT = null; 
    //TWITTER THINGS 
     static String TWITTER_CONSUMER_KEY = "CONSUMER_KEY_HERE"; 
     static String TWITTER_CONSUMER_SECRET = "CONSUMER_SECRET_HERE"; 
     static final String TWITTER_CALLBACK_URL = "oauth://t4jsample"; 

     // Twitter oauth urls 
     static final String URL_TWITTER_AUTH = "https://api.twitter.com/oauth/authorize"; 
     static final String URL_TWITTER_OAUTH_VERIFIER = "https://api.twitter.com/oauth/access_token"; 
     static final String URL_TWITTER_OAUTH_TOKEN = "https://api.twitter.com/oauth/request_token"; 

     // Progress dialog 
     ProgressDialog pDialog; 

     // Twitter 
     public static Twitter twitter; 
     public static String twitter_token, twitter_secret; 


     // Internet Connection detector 
     private ConnectionDetector cd; 

     // Alert Dialog Manager 
     AlertDialogManager alert = new AlertDialogManager(); 



    // Preference Constants 
    static String PREFERENCE_NAME = "twitter_oauth"; 
    static final String PREF_KEY_OAUTH_TOKEN = "oauth_token"; 
    static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret"; 
    static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn"; 



    // Twitter 

    private static RequestToken requestToken; 

    // Shared Preferences 
    private static SharedPreferences mSharedPreferences; 



    @SuppressLint("NewApi") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_twitter); 
     if (Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
     //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     cd = new ConnectionDetector(getApplicationContext()); 

     // Check if Internet present 
     if (!cd.isConnectingToInternet()) { 
      // Internet Connection is not present 
      alert.showAlertDialog(TwitterActivity.this, "Internet Connection Error","Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 

     // Check if twitter keys are set 
     if(TWITTER_CONSUMER_KEY.trim().length() == 0 || TWITTER_CONSUMER_SECRET.trim().length() == 0){ 
      // Internet Connection is not present 
      alert.showAlertDialog(TwitterActivity.this, "Twitter oAuth tokens", "Please set your twitter oauth tokens first!", false); 
      // stop executing code by return 
      return; 
     } 


     // Shared Preferences 
     mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0); 



     ImageView TW = (ImageView) findViewById(R.id.twitter_boton); 
     TW.setClickable(true); 
     TW.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       loginToTwitter(); 
      } 
     }); 


     /** This if conditions is tested once is 
     * redirected from twitter page. Parse the uri to get oAuth 
     * Verifier 
     * */ 
     if (!isTwitterLoggedInAlready()) { 
      Uri uri = getIntent().getData(); 
      if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) { 
       // oAuth verifier 
       String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER); 

       try { 
        // Get the access token 
        AccessToken accessToken = twitter.getOAuthAccessToken(
          requestToken, verifier); 

        // Shared Preferences 
        Editor e = mSharedPreferences.edit(); 

        // After getting access token, access token secret 
        // store them in application preferences 
        e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken()); 
        e.putString(PREF_KEY_OAUTH_SECRET, 
          accessToken.getTokenSecret()); 
        // Store login status - true 
        e.putBoolean(PREF_KEY_TWITTER_LOGIN, true); 
        e.commit(); // save changes 

        Log.e("Twitter OAuth Token", "> " + accessToken.getToken()); 



        // Getting user details from twitter 
        // For now i am getting his name only 
        long userID = accessToken.getUserId(); 
        User user = twitter.showUser(userID); 
        String username = user.getName(); 
        Log.d("nombre",username); 
       } catch (Exception e) { 
        // Check log for login errors 
        Log.e("Twitter Login Error", "> " + e.toString()); 
        Log.e("Twitter Login Error", "> " + e.getMessage()); 
       } 
      } 
     } 

    } 

    /** 
    * Function to login twitter 
    * */ 
    private void loginToTwitter() { 
     // Check if already logged in 
     if (!isTwitterLoggedInAlready()) { 
      ConfigurationBuilder builder = new ConfigurationBuilder(); 
      builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
      builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); 
      Configuration configuration = builder.build(); 

      TwitterFactory factory = new TwitterFactory(configuration); 
      twitter = factory.getInstance(); 

      if(!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)) { 
       try { 
        requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL); 
        this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL()))); 
       } catch (TwitterException e) { 
        e.printStackTrace(); 
       } 
      } 
      else 
      { 
       new Thread(new Runnable() { 
        public void run() { 
         try { 
          requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL); 
          TwitterActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL()))); 
         } catch (TwitterException e) { 
          e.printStackTrace(); 
         } 
        } 
       }).start(); 
      } 
     } else { 
      // user already logged into twitter 
      Toast.makeText(getApplicationContext(), 
        "Already Logged into twitter", Toast.LENGTH_LONG).show(); 
     } 
    } 

    /** 
    * Function to update status 
    * */ 
    class updateTwitterStatus extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(TwitterActivity.this); 
      pDialog.setMessage("Updating to twitter..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting Places JSON 
     * */ 
     protected String doInBackground(String... args) { 
      Log.d("Tweet Text", "> " + args[0]); 
      String status = args[0]; 
      try { 
       ConfigurationBuilder builder = new ConfigurationBuilder(); 
       builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
       builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); 

       // Access Token 
       String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, ""); 
       // Access Token Secret 
       String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, ""); 

       AccessToken accessToken = new AccessToken(access_token, access_token_secret); 
       Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken); 

       // Update status 
       twitter4j.Status response = twitter.updateStatus(status); 

       Log.d("Status", "> " + response.getText()); 
      } catch (TwitterException e) { 
       // Error in updating status 
       Log.d("Twitter Update Error", e.getMessage()); 
      } 
      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog and show 
     * the data in UI Always use runOnUiThread(new Runnable()) to update UI 
     * from background thread, otherwise you will get error 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Status tweeted successfully", Toast.LENGTH_SHORT) 
          .show(); 
        // Clearing EditText field 

       } 
      }); 
     } 

    } 



    /** 
    * Check user already logged in your application using twitter Login flag is 
    * fetched from Shared Preferences 
    * */ 
    private boolean isTwitterLoggedInAlready() { 
     // return twitter login status from Shared Preferences 
     return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false); 
    } 

    protected void onResume() { 
     super.onResume(); 
    } 

} 

回答

4

花費數小時閱讀文檔(我沒有現在的視力),當我試圖讓我的access_token調用此之後:

// Get the access token 
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); 

我通過了驗證器(這是pin傳遞,但pin傳遞不再使用),他是null,因爲不再使用twitter,那麼我只需要用下面的代碼重寫:

// Get the access token 
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); 

只需要requestToken即可獲取access_token。

我希望這可以幫助任何有同樣問題的人。

Grettings。

1

我看了你的代碼(由於類似的問題),發現一個錯誤。 看,當焦點再次返回到應用程序時,在收到requestToken後,您嘗試獲取具有錯誤常量值的oAuth verifier

String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER); 

其中URL_TWITTER_OAUTH_VERIFIER在你的代碼是

URL_TWITTER_OAUTH_VERIFIER = "https://api.twitter.com/oauth/access_token" (!) 

,但你必須在其他getQueryParameter(OAUTH_VERIFIER);

OAUTH_VERIFIER = = "oauth_verifier"; 

設置和你的代碼變得完全正確的!

JosephCastro對我提出的解決方案無效。