I try to access information from Twitter and I followed this link:
http://code.google.com/p/sociallib/wiki/SocialLibGuide
I don’t understand the following two lines and it shows error in the following lines.
twitter.requestAuthorization(this);
twitter.authorize(this);
Full code is added below.
It says that anroid.content.Context and android.app.Activity is required. I really don’t know how to add them. Any help is appreciated.
package sociallibjar;
import android.R;
import android.content.Context;
import com.expertiseandroid.lib.sociallib.connectors.SocialNetworkHelper;
import com.expertiseandroid.lib.sociallib.connectors.TwitterConnector;
import com.expertiseandroid.lib.sociallib.model.twitter.TwitterUser;
import org.scribe.oauth.Token;
public class TwitterApp {
String CONS_KEY = "";
String CONS_SEC = "";
String CALLBACK = "";
public void twiter() {
try {
TwitterConnector twitter = SocialNetworkHelper.createTwitterConnector(CONS_KEY, CONS_SEC, CALLBACK);
twitter.requestAuthorization(this);
twitter.authorize(this);
Token at = twitter.getAccessToken();
String token = at.getToken(); //You can store these two strings
String secret = at.getSecret();//in order to build the token back
Token myAccessToken = new Token(token, secret);
twitter.authentify(myAccessToken);
TwitterUser me = twitter.getUser(); //Retrieves the current user
String nickname = me.getUsername(); //Some information is available through method calls
int nbFollowers = me.nbFollowers; //Some is available through object fields
System.out.println("You are logged in as " + nickname + " and you have " + nbFollowers + " followers"); //Now we can print the information we retrieved onscreen
twitter.tweet("Tweeting from code"); //A simple tweet
twitter.tweet("I have " + twitter.getUser().nbFollowers + " followers!");//A more elaborate tweet
//twitter.logout(this); //Providing this code is located in an Activity (or Context) class
} catch (Exception e) {
}
}
}
The documentation from that link it is pretty clear. For the lines:
thisrepresents aContext(anActivityfor example).Now, because your class
TwitterAppdoesn’t extendActivityyou’ll need a reference toContextto provide to those methods. For this you could, for example, add a constructor to yourTwitterAppclass that takes aContext:and then you use that context for those methods:
In your activity where you instantiate the
TwitterAppclass just passthis: