मैं oauth2 (केवल एप्लिकेशन प्रमाणीकरण के लिए) के साथ ट्विटर से उपयोगकर्ता टाइमलाइन प्राप्त करने का प्रयास कर रहा हूं लेकिन परिणाम हमेशा शून्य है। मुझे ओथ के साथ अनुभव नहीं है, और मैंने कुछ ट्यूटोरियल और उदाहरणों को देखा है, लेकिन अब तक कोई किस्मत नहीं है। मुझे उपयोगकर्ता को लॉगिन करने की आवश्यकता के बिना एक विशिष्ट उपयोगकर्ता टाइमलाइन (इस उदाहरण में ट्विटर) को पुनः प्राप्त करने की आवश्यकता है।ट्विटर एप्लिकेशन केवल प्रमाणीकरण जावा एंड्रॉइड twitter4j
मेरे पास एक ऐसा एप्लिकेशन था जिसने ट्विटर एपीआई 1 का उपयोग किया था और मैंने अपने मौजूदा कोड को नए एपीआई 1.1 में ओएथ 2 के साथ केवल एप्लिकेशन के लिए अनुकूलित करने का प्रयास किया था। तो कोड को काम करना चाहिए सिवाय इसके कि इसे ट्विटर से कुछ भी वापस नहीं मिलता है। अगर मुझे परिणाम मिल जाए तो इसे फिर से काम करना चाहिए।
ट्विटर के साथ कनेक्शन एसिंकॉप के नीचे फ़ंक्शन ट्विटरकनेक्ट में बनाया गया है।
public class TwitterActivity extends Activity {
private ConfigurationBuilder builder;
// twitter consumer key and secret
static String TWITTER_CONSUMER_KEY = "**************";
static String TWITTER_CONSUMER_SECRET = "*************";
// twitter acces token and accestokensecret
static String TWITTER_ACCES_TOKEN = "************";
static String TWITTER_ACCES_TOKEN_SECRET = "************";
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitter);
new AsyncOp().execute("test");
}
//Async class...
public class AsyncOp extends AsyncTask<String, Void,List<twitter4j.Status>> {
protected List<twitter4j.Status> doInBackground(String... urls) {
//auth with twitter
List<twitter4j.Status> statuses = null;
try {
Twitter twitter=twitterConnect();
statuses = twitter.getUserTimeline("Twitter");
return statuses;
} catch (Exception ex) {
Log.d("Main.displayTimeline", "" + ex.getMessage());
}
return statuses;
}
protected void onPostExecute(List<twitter4j.Status> statuses) {
try {
String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy"; SimpleDateFormat sf=new
SimpleDateFormat(TWITTER, Locale.ENGLISH); sf.setLenient(true);
for(int i=0;i<statuses.size();i++){
twitter4j.Status stat = statuses.get(i);
User user=stat.getUser();
Date datePosted=stat.getCreatedAt();
String text=stat.getText();
String name=user.getName();
String profile_image_url=user.getProfileImageURL();
Tweet t =new Tweet(datePosted,text,name,profile_image_url,twitterHumanFriendlyDate(datePosted));
tweets.add(t);
// logcat info
Log.i("date",datePosted.toString());
Log.i("text",text);
Log.i("user",name);
Log.i("userprofilepic",profile_image_url);
Log.i("timeofpost",twitterHumanFriendlyDate(datePosted));
}
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(TwitterActivity.this,
R.layout.listitem, tweets));
ProgressBar bar=(ProgressBar) findViewById(R.id.progressBar1);
bar.setVisibility(View.GONE);
} catch
(Exception e) { e.printStackTrace(); } }
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int textViewResourceId,
ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView message = (TextView) v.findViewById(R.id.message);
ImageView image = (ImageView) v.findViewById(R.id.avatar);
TextView date = (TextView) v.findViewById(R.id.date);
if (username != null) {
username.setText(tweet.username);
}
if (message != null) {
message.setText(tweet.message);
}
if (image != null) {
image.setImageBitmap(getBitmap(tweet.image_url));
}
if (date != null) {
date.setText(tweet.hfdate);
}
}
return v;
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (Exception ex) {
return null;
}
}
// build twitter
public Twitter twitterConnect() throws Exception
{
// setup
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY).setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
OAuth2Token token = new TwitterFactory(builder.build()).getInstance().getOAuth2Token();
// exercise & verify
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setUseSSL(true);
cb.setApplicationOnlyAuthEnabled(true);
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
twitter.setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
twitter.setOAuth2Token(token);
return twitter;
}
public String twitterHumanFriendlyDate(Date dateCreated) {
// parse Twitter date
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH);
dateFormat.setLenient(false);
Date created = dateCreated;
// today
Date today = new Date();
// how much time since (ms)
Long duration = today.getTime() - created.getTime();
long second = 1000;
long minute = second * 60;
long hour = minute * 60;
long day = hour * 24;
if (duration < second * 7) {
return "right now";
}
if (duration < minute) {
int n = (int) Math.floor(duration/second);
return n + " seconds ago";
}
if (duration < minute * 2) {
return "about 1 minute ago";
}
if (duration < hour) {
int n = (int) Math.floor(duration/minute);
return n + " minutes ago";
}
if (duration < hour * 2) {
return "about 1 hour ago";
}
if (duration < day) {
int n = (int) Math.floor(duration/hour);
return n + " hours ago";
}
if (duration > day && duration < day * 2) {
return "yesterday";
}
if (duration < day * 365) {
int n = (int) Math.floor(duration/day);
return n + " days ago";
} else {
return "over a year ago";
}
}
}
मेरी समस्या बुद्धि OAuth विधि है:
यहाँ मेरी कोड है? या शायद getusertimeline बुद्धि? अगर किसी के पास twitter4j के साथ oauth2 के लिए कुछ उदाहरण कोड या ट्यूटोरियल है, तो समस्या की खोज के एक लंबे दिन के बाद
से काम करने के लिए एक संक्षिप्त समाधान प्रदान करने में मदद कर सकते हैं, क्या आप मुझे "ट्विटर फ़ीड लाने" का पूरा कामकाजी कोड दे सकते हैं? मुझे समस्याएं आ रही हैं –
'विधि सेट एप्प्लिकेशनऑनलीएथएनेबल (बूलियन) कॉन्फ़िगरेशनबिल्डर' – jul
@jul डाउनलोड के लिए अपरिभाषित है और यहां से नवीनतम ट्विटर 4 जे लाइब्रेरी स्नैपशॉट जोड़ें [http://twitter4j.org/en/index.html#snapshot ](http : //twitter4j.org/en/index.html#snapshot) – Mani