2

I am developing Android App interacting with Twitter using Fabric and Retrofit2 libraries. I want to display search timeline. My request URL is like this: https://api.twitter.com/1.1/friends/list.json?screen_name=xxx

The response body I got is null but I got the alert of bad authentication:215 and http error 400 in the debug mode.This is probably caused by invalid authentication of the request from my app.

The Twitter developer document said requests need to be authorized with OAuth and SSL certificated.

As for the OAuth issue, I wrote the request header based on the official document of twitter developer platform https://dev.twitter.com/oauth/overview/authorizing-requests and create the header with okhttpclient and pass it to retrofit object. The code for OAuth issue is like this.

public class TwitterClientApiClient extends TwitterApiClient {

private static final String TAG=TwitterClientApiClient.class.getSimpleName();

private static final MainApplication app=MainApplication.getInstance();

public static final String BASE_URL = "https://api.twitter.com/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {

    final String authStr = app.authStr();

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
                                  @Override
                                  public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                                      Request original = chain.request();

                                      Request request = original.newBuilder()

                                              .header("Accept", "application/json")
                                              .header("Authorization", authStr)
                                              .method(original.method(), original.body())
                                              .build();

                                      Headers okHeaders = request.headers();

                                      Log.d(TAG,okHeaders.toString());
                                      return chain.proceed(request);
                                  }
                              });

            OkHttpClient client = httpClient.build();

    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofit;
}

public TwitterClientApiClient(TwitterSession session) {
    super(session);
}

public FriendsService getFriendsService() {return getService(FriendsService.class);}


}

interface FriendsService {
    @GET("/1.1/friends/list.json")
    Call<FriendsResult> list(@Query("screen_name") String screen_name);
}

The following is the code making the request.

FriendsService apiService =
                TwitterClientApiClient.getClient().create(FriendsService.class);

        Call<FriendsResult> call = apiService.list(screenName);
        Log.d(TAG, call.request().url().toString());

        call.enqueue(new Callback<FriendsResult>() {
            @Override
            public void onResponse(Call<FriendsResult> call, Response<FriendsResult> response) {

                //List<User> friends = response.body().getUsers();

                Log.d(TAG,response.body().toString());
                //Log.d(TAG, "Number of Friends: " + friends.size());
                //String q = getQueryStr(friends);
                //showSearchedTimeline(q);
            }

            @Override
            public void onFailure(Call<FriendsResult>call, Throwable t) {

                Log.e(TAG, t.toString());
            }
        });

However,according to https://oauth.net/core/1.0/#encoding_parameters

OAuth Authentication is done in three steps:

1.The Consumer obtains an unauthorized Request Token. 2.The User authorizes the Request Token. 3.The Consumer exchanges the Request Token for an Access Token.

My code which is based on references from the internet seems to do only Step 3 and thus the authentication is not complete. I wonder how to complete the whole authentication process of OAuth.

Also do I need to do sth in my code for SSL stuff?

Besides OAuth and SSL, any other security issue for request to twitter server I have overlooked?

Thanks in advance!

Anndexi9
  • 141
  • 2
  • 15
  • Possible duplicate of [Auth 1.0 oauth\_signature creation Android for magento API](https://stackoverflow.com/questions/49505888/auth-1-0-oauth-signature-creation-android-for-magento-api) – Subin Babu Apr 21 '18 at 08:41

1 Answers1

2

.header("Authorization", authStr)

Try with addHeader. You can activate the logs (useful to debug sometimes) using a logging interceptor. Ask the logger to show your headers, to see if that could be the problem. Available levels are here.

Community
  • 1
  • 1
Gordak
  • 2,060
  • 22
  • 32