【问题标题】:OAuth issue of request from Android app with Retrofit & Fabric使用 Retrofit & Fabric 的 Android 应用程序请求的 OAuth 问题
【发布时间】:2017-03-25 10:10:25
【问题描述】:

我正在使用 Fabric 和 Retrofit2 库开发与 Twitter 交互的 Android 应用。我想显示搜索时间线。我的请求网址是这样的:https://api.twitter.com/1.1/friends/list.json?screen_name=xxx

我得到的响应正文为空,但在调试模式下我得到了错误身份验证的警报:215 和 http 错误 400。这可能是由于我的应用程序请求的身份验证无效造成的。

Twitter 开发人员文档称请求需要通过 OAuth 和 SSL 证书进行授权。

关于OAuth问题,我根据twitter开发者平台https://dev.twitter.com/oauth/overview/authorizing-requests的官方文档写了请求头 并使用 okhttpclient 创建标头并将其传递给改造对象。 OAuth问题的代码是这样的。

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);
}

以下是发出请求的代码。

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());
            }
        });

但是,根据https://oauth.net/core/1.0/#encoding_parameters

OAuth 身份验证分三步完成:

1.Consumer 获得未经授权的Request Token。 2.用户授权请求令牌。 3.Consumer用Request Token换取Access Token。

我基于互联网参考的代码似乎只执行第 3 步,因此身份验证不完整。我想知道如何完成OAuth的整个认证过程。

我还需要在我的代码中为 SSL 做某事吗?

除了 OAuth 和 SSL 之外,我还忽略了对 twitter 服务器的请求的其他安全问题吗?

提前致谢!

【问题讨论】:

标签: android security oauth network-programming retrofit


【解决方案1】:

.header("授权", authStr)

试试addHeader。您可以使用logging interceptor 激活日志(有时对调试有用)。让记录器显示您的标题,看看这是否可能是问题所在。可用级别为here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多