2012-12-02 14 views
5

मैं twitter4j लाइब्रेरी के साथ एक ट्विटर क्लाइंट बनाने की कोशिश कर रहा हूं। लेकिन फिर भी मैं सामान से स्पष्ट नहीं हूं और मुझे एक अच्छा ट्यूटोरियल नहीं मिला। अधिकांश ट्यूटोरियल पुराने हैं। मुख्य रूप से मैं यह जानना चाहता हूं, क्या मुझे ट्विटर क्लाइंट बनाने पर हर बार OAuth का उपयोग करना होगा? यदि नहीं, तो मुझे यह कैसे करना चाहिए (मेरा मतलब है, 'उपभोक्ता-कुंजी' और 'उपभोक्ता-रहस्य' प्राप्त किए बिना और केवल उपयोगकर्ता नाम और पासवर्ड दर्ज करके)? किसी भी सहायता की सराहना की जाएगी। धन्यवाद।Android के लिए Twitter4j लाइब्रेरी का उपयोग

+0

किसी भी बात आप की कोशिश? –

उत्तर

12

आप twitter4j उपयोग करना चाहते हैं http://dev.twitter.com/apps/ किसी ऐप रजिस्टर करने के लिए एक उपभोक्ता कुंजी और रहस्य निकलना है।

मेरा ऐप बस एक छवि के साथ एक ट्वीट पोस्ट और मैं एक साधारण ट्वीट समारोह को खोजने के लिए उम्मीद कर रही थी लेकिन इसके बजाय प्रमाणीकरण विवरण और कोड सूत्रण मुद्दों की एक भूलभुलैया पाया।

यह आसान सिर्फ एक छवि मैं एक आवरण JAR फ़ाइल है कि सभी प्रमाणीकरण और सूत्रण मुद्दों संभालती अंदर twitter4j समझाया है के साथ ट्वीट को बनाने के लिए। काम करने के लिए इसे कोड की कुछ पंक्तियों (न्यूनतम पर 3) की आवश्यकता होती है। जेएआर फ़ाइल को MSTwitter.jar कहा जाता है।

MSTwitter.jar में तीन फाइलें हैं जिनमें से एक एमएसटीविटर है। इस फ़ाइल के शीर्ष पर टिप्पणियां हैं जो बताती हैं कि जेएआर का उपयोग कैसे करें। उन्हें यहां दोहराया जाता है:

To use MSTwitter.jar: 
-Project setup:  
    -Create a twitter app on https://dev.twitter.com/apps/ to get:  
     -CONSUMER_KEY a public key(string) used to authenticate your app with Twitter.com  
     -CONSUMER_SECRET a private key(string) used to authenticate your app with Twitter.com  
     You don't need any thing else so authorization url etc are not important for this process 
    -Put twitter4j-core-3.0.2.jar and MSTwitter.jar files in your project's libs directory:   
     -You can download twitter4j from from http://twitter4j.org 
    -Register the jars in your project build path: 
     Project->Properties->Java Build Path->Libraries->Add Jar   
     ->select the jar files you just added to your project's libs directory. 
    -Make AndroidManifest.xml modifications   
    -Add <uses-permission android:name="android.permission.INTERNET" /> inside manifest section (<manifest>here</manifest>) 
    -Add <uses-permission android:name="com.mindspiker.mstwitter.MSTwitterService" /> inside manifest section 
    -Add <uses-permission android:name="android.permission.BROADCAST_STICKY" /> inside manifest section 
    -Add <activity android:name="com.mindspiker.mstwitter.MSTwitterAuthorizer" /> inside application section. 
    -Add <service android:name="com.mindspiker.mstwitter.MSTwitterService" /> inside application section. 

-Code to add to you calling activity 
    -Define a module MSTwitter variable. 
    -In onCreate() allocate a module level MSTwitter variable 
     ex: mMTwitter = new MSTWitter(args);] 
    -Add code to catch response from MSTwitterAuthorizer in your activity's onActivityResult() 
     ex:@Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ mMSTwitter.onCallingActivityResult(requestCode, resultCode, data); } 
    -Call startTweet(String text, String imagePath) on your MSTwitter object instance. 
     if you image is held in memory and not saved to disk call 
     MSTwitter.putBitmapInDiskCache() to save to a temporary file on disk. 
    -(Optional) Add a MSTwitterResultReceiver to catch MSTwitter events which are 
     fired at various stages of the process. 
     MSTwitterResultReceiver events: 
      -MSTWEET_STATUS_AUTHORIZING the app is not authorized and the authorization process is starting. 
      -MSTWEET_STATUS_STARTING the app is authorized and sending the tweet text and image is starting. 
      -MSTWEET_STATUS_FINSIHED_SUCCCESS the tweet is done and was successful.   
      -MSTWEET_STATUS_FINSIHED_FAILED the tweet is done and failed to complete.  
Notes: 
-If your project compiles but crashes when any twwiter4j object is instantiated a possible cause may be trying to add the jars as external jars instead of the suggested method above. If your libraries directory already exists but is called 'lib', change the name to 'libs'. Sounds crazy I know, but works in some situations. 
-To prevent large images from being passed around between intents images should be cached to disk and retrieved when used. Only the file name is passed between intents. 
-To perform tasks on a separate thread that passes information to and from activities, which could be destroyed at any time (phone call, screen orientation change, etc.), a method using intent services to perform the work and sticky broadcasts to pass the  data was employed. For more on this method and why it was used check out: http://stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-thre/8074278#8074278 

नीचे दी गई फ़ाइलें एक नमूना प्रोजेक्ट से हैं जो एक छवि के साथ एक ट्वीट पोस्ट करती है।

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.imagetweettester" android:versionCode="1" android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="8"android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.mindspiker.mstwitter.MSTwitterService" /> 
    <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 
    <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > 
     <activity android:name="com.example.imagetweettester.MainActivity" android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.mindspiker.mstwitter.MSTwitterAuthorizer" /> 
     <service android:name="com.mindspiker.mstwitter.MSTwitterService" /> 
    </application> 
</manifest> 

MainActivity.java

package com.example.imagetweettester; 

import java.text.SimpleDateFormat; 
import java.util.Date; 

import com.mindspiker.mstwitter.MSTwitter; 
import com.mindspiker.mstwitter.MSTwitter.MSTwitterResultReceiver; 

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
/** Consumer Key generated when you registered your app at https://dev.twitter.com/apps/ */ 
public static final String CONSUMER_KEY = "yourConsumerKeyHere"; 
/** Consumer Secret generated when you registered your app at https://dev.twitter.com/apps/ */ 
public static final String CONSUMER_SECRET = "yourConsumerSecretHere"; 
/** module level variables used in different parts of this module */ 
private MSTwitter mMSTwitter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // setup button to call local tweet() function 
    Button tweetButton = (Button) findViewById(R.id.button1); 
    tweetButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      tweet(); 
     } 
    }); 

    // make a MSTwitter event handler to receive tweet send events 
    MSTwitterResultReceiver myMSTReceiver = new MSTwitterResultReceiver() { 
     @Override 
     public void onRecieve(int tweetLifeCycleEvent, String tweetMessage) { 
      handleTweetMessage(tweetLifeCycleEvent, tweetMessage); 
     } 
    }; 

    // create module level MSTwitter object. 
    // This object can be destroyed and recreated without interrupting the send tweet process. 
    // So no need to save and pass back in savedInstanceState bundle. 
    mMSTwitter = new MSTwitter(this, CONSUMER_KEY, CONSUMER_SECRET, myMSTReceiver); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    //This MUST MUST be done for authorization to work. If your get a MSTWEET_STATUS_AUTHORIZING 
    // message and nothing else it is most likely because this is not being done. 
    mMSTwitter.onCallingActivityResult(requestCode, resultCode, data); 
} 

/** 
* Send tweet using MSTwitter object created in onCreate() 
*/ 
private void tweet() { 
    // assemble data 

    // get text from layout control 
    EditText tweetEditText = (EditText) findViewById(R.id.editText1); 
    String textToTweet = tweetEditText.getText().toString(); 
    // get image from resource 
    Bitmap imageToTweet = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
    // use MSTwitter function to save image to file because startTweet() takes an image path 
    // this is done to avoid passing large image files between intents which is not android best practices 
    String tweetImagePath = MSTwitter.putBitmapInDiskCache(this, imageToTweet); 

    // start the tweet 
    mMSTwitter.startTweet(textToTweet, tweetImagePath); 
} 

@SuppressLint("SimpleDateFormat") 
private void handleTweetMessage(int event, String message) { 

    String note = ""; 
    switch (event) { 
    case MSTwitter.MSTWEET_STATUS_AUTHORIZING: 
     note = "Authorizing app with twitter.com"; 
     break; 
    case MSTwitter.MSTWEET_STATUS_STARTING: 
     note = "Tweet data send started"; 
     break; 
    case MSTwitter.MSTWEET_STATUS_FINSIHED_SUCCCESS: 
     note = "Tweet sent successfully"; 
     break; 
    case MSTwitter.MSTWEET_STATUS_FINSIHED_FAILED: 
     note = "Tweet failed:" + message; 
     break; 
    } 

    // add note to results TextView 
    SimpleDateFormat timeFmt = new SimpleDateFormat("h:mm:ss.S"); 
    String timeS = timeFmt.format(new Date()); 
    TextView resultsTV = (TextView) findViewById(R.id.resultsTextView); 
    resultsTV.setText(resultsTV.getText() + "\n[Message received at " + timeS +"]\n" + note); 
} 
} 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".MainActivity" > 
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Start Tweet" /> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text to tweet:" /> 
<EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:text="Test tweet text" > 
     <requestFocus /> 
    </EditText> 
    <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> 
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Results:" /> 
    <TextView android:id="@+id/resultsTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> 
</LinearLayout> 
+0

उत्तर माइंडस्पीकर के लिए धन्यवाद। मैं इसे आज़माउंगा ..... –

+1

हाय माइंडस्पिकर, क्या आप अपने इस lib को ओपन सोर्स के रूप में कहीं होस्ट करते हैं? यह ठीक काम करता है, लेकिन यह मुझे जो कुछ भी चाहिए उसे प्रदान नहीं करता है, इसलिए मैं इसे अपनी आवश्यकताओं के अनुसार कोड करना चाहता था ... –

+0

जार में स्रोत शामिल है ताकि आप इसे वहां से कॉपी कर सकें। यदि आप प्रोजेक्ट चाहते हैं तो आप http://www.mindspiker.com/Projects/MSTwitter/MSTwitter.zip पर डाउनलोड कर सकते हैं। – MindSpiker

1

मैं जार फ़ाइल MindSpiker के जवाब में संदर्भित प्रयोग किया है:

मैं तो मेरे चहचहाना अनुप्रयोग विन्यास में एक यादृच्छिक कॉलबैक URL डाल:

मेरा ऐप एक ब्राउज़र को खोलता है, मैं प्रवेश करें और फिर कॉलबैक यूआरएल यह ट्विटर के वेब के अंदर एक त्रुटि बनाने है कॉल करने के लिए कोशिश करता है।

अगर मैं छोड़ खाली है कि:

04-02 13:48:49.654: E/MSTwitter(7983): Tweeter Error: 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync. 
04-02 13:48:49.654: E/MSTwitter(7983): <?xml version="1.0" encoding="UTF-8"?> 
04-02 13:48:49.654: E/MSTwitter(7983): <hash> 
04-02 13:48:49.654: E/MSTwitter(7983): <request>/oauth/request_token</request> 
04-02 13:48:49.654: E/MSTwitter(7983): <error>Desktop applications only support the oauth_callback value 'oob'</error> 
04-02 13:48:49.654: E/MSTwitter(7983): </hash> 
04-02 13:48:49.654: E/MSTwitter(7983): 10f5ada3-e574402b: 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync. 
04-02 13:48:49.654: E/MSTwitter(7983): <?xml version="1.0" encoding="UTF-8"?> 
04-02 13:48:49.654: E/MSTwitter(7983): <hash> 
04-02 13:48:49.654: E/MSTwitter(7983): <request>/oauth/request_token</request> 
04-02 13:48:49.654: E/MSTwitter(7983): <error>Desktop applications only support the oauth_callback value 'oob'</error> 
04-02 13:48:49.654: E/MSTwitter(7983): </hash> 

क्या मैं गलत यहाँ कर रहा हूँ: मैं इस त्रुटि मिलती है?

+0

त्रुटि संदेश से ऐसा लगता है कि आपका उपभोक्ता और रहस्य सेट नहीं है। वे उपरोक्त गतिविधि फ़ाइल में सेट हैं जब MSTwitter ऑब्जेक्ट MSTwitter (यह, CONSUMER_KEY, CONSUMER_SECRET, myMSTReceiver) के साथ बनाया गया है। Dev.twitter.com/apps/ – MindSpiker

+0

माइंडस्पीइक पर उनके सिस्टम में एप्लिकेशन बनाकर ट्विटर से इन मानों को प्राप्त करने की आवश्यकता है: आपके उत्तर के लिए धन्यवाद। दुर्भाग्य से मैंने पहले से ही चाबियाँ सेट की थीं। मुझे dev.twitter.com/apps/ सही में कॉलबैक यूआरएल खाली छोड़ना है? – franck

+0

एमएसटीविटर में उपयोग किए गए कॉलबैक यूआरएल "कॉम-मस्तिष्क-एमस्टविटर" है, यह उपयोगकर्ता के आईडी आईडी और पासवर्ड के साथ अधिकृत होने पर अनुरोध यूआरएल प्राप्त करने के लिए प्रक्रिया के हिस्से के रूप में twitter.com को पास किया जाता है। प्रक्रिया MSTwitterService.processGetAuthURL() में होती है। चूंकि कॉल बैक यूआरएल को प्रक्रिया को खराब करने के लिए भेजा जाता है, मुझे नहीं लगता कि इसे dev.twitter.com/apps/ में कॉलबैक यूआरएल के रूप में जो कुछ भी दर्ज किया गया है उससे मेल खाना पड़ेगा। इसके अलावा बस मेरे ट्विटर ऐप की जांच की गई और मेरा कॉलबैक यूआरएल एमएसटीविटर जार में इस्तेमाल किए गए एक से संबंधित नहीं है। – MindSpiker

1

सबसे पहले मैं आपको इस भयानक रैपर के लिए माइंडस्पीकर धन्यवाद देना चाहता हूं। मैं विश्वास नहीं कर सकता कि एंड्रॉइड पर एक साधारण पाठ स्थिति कितनी मुश्किल है। आईओएस में सही लाइब्रेरी का उपयोग करके बहुत आसान है। और मैं आपकी लाइब्रेरी के साथ एक छोटे से फिक्स के साथ योगदान देना चाहता हूं।

कुछ उपयोगकर्ताओं की तरह मैं भी मिल गया "क्षमा करें कि पृष्ठ मौजूद नहीं है" वेबपेज के बाद मैं अपने ऐप्स को अधिकृत तो मैं अपने कोड डाउनलोड और स्रोत से डिबग। एक अजीब कारण के लिए MSTwitterAuthorizer.java फ़ाइल परOverrideUrlLoading विधि सही तरीके से ट्रिगर नहीं कर रही है।

private class TwitterWebViewClient extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // see if Twitter.com send back a url containing the callback key 

     if (url.contains(MSTwitter.CALLBACK_URL)) { 
      // we are done talking with twitter.com so check credentials and finish interface. 
      processResponse(url); 
      finish(); 
      return true; 
     } else { 
      // keep browsing 
      view.loadUrl(url); 
      return false; 
     } 
    } 
} 

मैं एक onPageStarted घटना जोड़ने के लिए सुझाव देते हैं:

private class TwitterWebViewClient extends WebViewClient { 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 

     if (url.contains(MSTwitter.CALLBACK_URL)) { 
      // we are done talking with twitter.com so check credentials and finish interface. 
      processResponse(url); 
      finish(); 
     } 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // see if Twitter.com send back a url containing the callback key 

     if (url.contains(MSTwitter.CALLBACK_URL)) { 
      // we are done talking with twitter.com so check credentials and finish interface. 
      processResponse(url); 
      finish(); 
      return true; 
     } else { 
      // keep browsing 
      view.loadUrl(url); 
      return false; 
     } 
    } 
} 

और फिर मैं यह एंड्रॉयड 2.2 पर काम :)

कर सकता है GitHub पर अपने कोड अपलोड करें यहाँ मूल कोड है , आप टाइमलाइन या कुछ और प्राप्त करने जैसे कुछ अतिरिक्त तरीकों को जोड़ सकते हैं। प्रमाणीकरण चीज बहुत परेशान है।

+1

धन्यवाद। यह परियोजना गिथब पर है, इसलिए यदि आप गिट हब का उपयोग करते हैं तो आप परिवर्तन कर सकते हैं और उन्हें धक्का दे सकते हैं। यदि नहीं, तो मैं उस परिवर्तन को शामिल करूंगा जब मैं कुछ अन्य परिवर्तनों के आसपास पहुंच जाऊंगा जो लोग इसका इस्तेमाल करते थे। जिथब प्रोजेक्ट http://github.com/mindSpiker/MSTwitter पर स्थित है। – MindSpiker

+0

मैंने आपके कोड पर एक और फिक्स किया (मुझे ब्रॉडकास्टर के लिए रिसाव मिल रही थी)। मैं इसे एक पूर्ण ट्विटर रैपर बनाने के बारे में सोच रहा हूं। – noisedan

1

आह, आप भी कॉलबैक यूआरएल सेट करने के लिए (किसी भी वेबपेज आप चाहते हैं, यह कोई बात नहीं है) चहचहाना देव एप्लिकेशन सेटिंग पृष्ठ में की जरूरत है :)

संबंधित मुद्दे