android改造获取访问令牌(错误请求错误)

时间:2017-01-16 20:44:41

标签: android retrofit2 access-token

postman

这是我的邮递员和访问令牌。 如何使用改造获取访问令牌 在postman身体参数改造或Request.Builder等于什么? 我收到了“错误请求”

我的ServiceGenerator: (我在改造中使用基本的auth)

public class ServiceGenerator {
public static final String API_BASE_URL = "http://10.0.2.2:11050/";

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());


public static <S> S createService(Class<S> serviceClass, String username, String password) {
    if (username != null && password != null) {
        String credentials = username + ":" + password;
        final String basic =
                "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

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

                Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", "Basic Og==")
                        .header("Accept", "application/json")
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }

    OkHttpClient client = httpClient.build();
    Retrofit retrofit = builder.client(client).build();
    return retrofit.create(serviceClass);
}

我的服务界面:

public interface LoginService {

@FormUrlEncoded
@POST("/token")
Call<AccessToken> getAccessToken(
        @Field("grant_type") String grant_type);

}
  

onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    init();


    loginService = ServiceGenerator.createService(LoginService.class,"ss","ss");

    callAcc = loginService.getAccessToken("password");

    callAcc.enqueue(callAccCallBack);
    AccessToken accessToken;
    private Callback<AccessToken> callAccCallBack = new    Callback<AccessToken>() {
    @Override
    public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
        if(response.isSuccessful()){
            accessToken = response.body();
            edt_editText.setText(accessToken.getAccessToken());
        }else{
            Toast.makeText(Login.this, response.message(), Toast.LENGTH_LONG).show();
        }

    }

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

    }
};

0 个答案:

没有答案