Skip to content

Instantly share code, notes, and snippets.

@billynyh
Last active May 29, 2020 07:25
Show Gist options
  • Save billynyh/9e96d228b0e64c7c3251 to your computer and use it in GitHub Desktop.
Save billynyh/9e96d228b0e64c7c3251 to your computer and use it in GitHub Desktop.
Android integration of retrofit, signpost, twitter api
public class ApiModule {
private TwitterApi mApi;
public void init(String token, String tokenSecret) {
OAuthConsumer consumer = new DefaultOAuthConsumer(
Config.TWEET_API_KEY,
Config.TWEET_API_SECRET);
consumer.setTokenWithSecret(token, tokenSecret);
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.twitter.com/1.1/")
.setClient(new SignedOkClient(consumer))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
mApi = restAdapter.create(TwitterApi.class);
}
public List<Tweet> getUserTimeline() {
return mApi.getUserTimeline("abt_programming", Config.PER_PAGE);
}
}
public class SignedOkClient extends OkClient {
private static final String TAG = "SignedOkClient";
private OAuthConsumer mConsumer =null;
public SignedOkClient(OAuthConsumer consumer) {
super();
mConsumer = consumer;
}
@Override
protected HttpURLConnection openConnection(Request request)
throws IOException {
HttpURLConnection connection = super.openConnection(request);
try {
HttpRequest signedReq = mConsumer.sign(connection);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
return connection;
}
}
public interface TwitterApi {
@GET("/statuses/user_timeline.json")
List<Tweet> getUserTimeline(
@Query("screen_name") String screenName,
@Query("count") int count
);
}
public class TwitterAuthActivity extends BaseActivity {
private static final String TAG = "TwitterAuthActivity";
private final static String CALLBACK = "oauth://twitter";
private WebView mWebView;
private String mAuthUrl;
private OAuthProvider mProvider;
private OAuthConsumer mConsumer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_auth);
initTwitter();
mWebView = (WebView)findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("oauth")){
Uri uri = Uri.parse(url);
onOAuthCallback(uri);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
if (TextUtils.isEmpty(mAuthUrl)) {
mWebView.loadUrl(mAuthUrl);
}
}
@Override
protected void onStart() {
super.onStart();
}
private void initTwitter() {
mConsumer = new DefaultOAuthConsumer(
Config.TWEET_API_KEY,
Config.TWEET_API_SECRET);
mProvider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
mAuthUrl = mProvider.retrieveRequestToken(mConsumer, CALLBACK);
Log.d(TAG, "mAuthUrl " + mAuthUrl);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void v) {
if (!TextUtils.isEmpty(mAuthUrl)) {
mWebView.loadUrl(mAuthUrl);
}
}
}.execute();
}
private void onOAuthCallback(final Uri uri) {
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... voids) {
String pinCode = uri.getQueryParameter("oauth_verifier");
try {
mProvider.retrieveAccessToken(mConsumer, pinCode);
String token = mConsumer.getToken();
String tokenSecret = mConsumer.getTokenSecret();
PrefUtil.setTwitterToken(getActivity(), token);
PrefUtil.setTwitterTokenSecret(getActivity(), tokenSecret);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
finish();
}
}.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment