Skip to content

Instantly share code, notes, and snippets.

@mrhether
Created May 3, 2017 14:11
Show Gist options
  • Save mrhether/aad567df0d52b0c953beb33678d82c73 to your computer and use it in GitHub Desktop.
Save mrhether/aad567df0d52b0c953beb33678d82c73 to your computer and use it in GitHub Desktop.
Twitter
dependencies {
...
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
...
}
TwitterServiceBuilder.getTwitterService().search("test").enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(TwitterActivity.this, response.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(TwitterActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});
package your.package.name;
import com.google.gson.annotations.SerializedName;
import java.io.IOException;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.Route;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Query;
/**
* Created by markhetherington on 2017-05-02.
*/
public class TwitterServiceBuilder {
public static final String TOKEN = "INSERT_TOKEN";
public static final String SECRET = "INSERT_SECRET";
public static class AuthToken {
@SerializedName("token_type")
private String tokenType;
@SerializedName("access_token")
private String accessToken;
public String getBearer() {
return "Bearer " + accessToken;
}
}
public interface TwitterService {
@GET("/1.1/search/tweets.json?include_entities=true")
Call<ResponseBody> search(@Query("q") String search);
@POST("/oauth2/token?grant_type=client_credentials")
Call<AuthToken> auth(@Header("Authorization") String credentials);
}
private static TwitterService twitterService;
private static AuthToken savedAuthToken = new AuthToken();
public static TwitterService getTwitterService() {
if (twitterService == null) {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new AuthInterceptor())
.authenticator(new AuthAuthenticator())
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl("https://api.twitter.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
twitterService = retrofit.create(TwitterService.class);
}
return twitterService;
}
/**
* Adds an Authorization Header to each request made
*/
private static class AuthInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Authorization", savedAuthToken.getBearer()).build();
return chain.proceed(request);
}
}
/**
* If a network call returns an HTTP 401 this method is called,
* it will then create request an auth token, save it and retry the request
*/
private static class AuthAuthenticator implements Authenticator {
@Override
public Request authenticate(Route route, Response response) throws IOException {
savedAuthToken = getTwitterService().auth(Credentials.basic(TOKEN, SECRET)).execute().body();
return response.request().newBuilder().header("Authorization", savedAuthToken.getBearer()).build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment