如何使用OAuth2在改造中​​添加标头?

时间:2019-02-10 17:06:15

标签: android

我正在开发一个应用程序。我正在使用Retrofit2进行API调用。我必须使用OAuth2添加标头。但是我不知道该怎么办? 这里是 键 “ uuid” “ profile_pic”

,并且我要求为标题添加OAuth2。必须使用一些令牌。但是我不清楚我是否必须在标头中传递令牌或对OAuth2使用其他方式

public interface ApiInterface {

@Headers("Accept: application/json")
@FormUrlEncoded
@POST("user/profile-pic")
Call<Model> updateProfilePic(@Field("uuid") String uuid,
                             @Field("profile_pic") String profilePic,
                             @Header("OAuth2") String OAuth2);

}

class APIClient {

static Retrofit getClient() {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();



    return retrofit;
}

}

private void updateUserPic() {

    SharedPreferences sp = getActivity().getSharedPreferences("UserData" ,Context.MODE_PRIVATE);
    String uuid  = sp.getString("User_Uuid",null);
    String token = sp.getString("User_Token", null);

    Log.v("uuid", uuid);

    Call<Model> call =
            apiInterface.updateProfilePic(uuid, profilePic, token);
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {

            if (response.isSuccessful()) {

                Log.v("Response", response.message());

            }
        }

        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            call.cancel();
        }
    });

}

0 个答案:

没有答案