私はアプリケーションでログアウトするretrofit2を使用していますが、毎回それがerror406が います:許容できない:ユーザーがありますログインしていない。 。私は改造カスタム ヘッダー認証を使用しています。ここに私のコードです:アンドロイド改造後のログアウト要求を与えるエラー406は許容できない:ユーザーがログインしていない...郵便配達APIに正常に動作します
ログアウトコード
ます。public voidログアウト()
{ Log.v( "checkTokenbefore"、Constants.token)。
OkHttpClient httpClient1 = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Log.v("checkLogin",Constants.token+Constants.username+Constants.password) ;
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept-Language","application/json").addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("API_KEY", "a5XSE8XCdsY6hAoCNojYBQ")
.addHeader("X-CSRF-Token",Constants.token)
;
Request request = requestBuilder.method(original.method(),original.body()).build();
return chain.proceed(request);
}
}).build();
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl(Constants.API_BASE_URL)
.client(httpClient1)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface restAPI1 = retrofit1.create(ApiInterface.class);
Call<Logout> callLogout = restAPI1.userLogout(Constants.token,Constants.username,Constants.password);
callLogout.enqueue(new Callback<Logout>() {
@Override
public void onResponse(Call<Logout> call, retrofit2.Response<Logout> response) {
Log.v("responseLogout",response.code()+"code"+response.errorBody().toString()+response.message()) ;
}
@Override
public void onFailure(Call<Logout> call, Throwable t) {
}
});
}
後は正常に動作し、ログイン用のコードですが:
public void loginQuestin(){
//checkValidation();
/*
ApiInterface apiService =
ApiClient.create(ApiInterface.class) ;*/
ApiInterface restAPI = retrofit.create(ApiInterface.class);
Call<UserAgain> call = restAPI.userLogin(mEmailAddress.getText().toString().trim(),
mPassword.getText().toString().trim());
call.enqueue(new Callback<UserAgain>() {
@Override
public void onResponse(Call<UserAgain> call, Response<UserAgain> response) {
Log.v("check",response.code()+"login"+response.body().getToken()) ;
//response.body().getU
Constants.username = mEmailAddress.getText().toString().trim() ;
Constants.password = mPassword.getText().toString().trim() ;
if (response.code()==200) {
Log.v("checkAgain",response.code()+"login") ;
Constants.token = response.body().getToken() ;
startActivity(new Intent(LoginActivity.this, NavigationDrawerActivity.class));
}
}
@Override
public void onFailure(Call<UserAgain> call, Throwable t) {
Log.v("check","failed");
t.printStackTrace();
}
});
}
//ログインAPI呼び出し
public class ApiClient {
public static OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder() .addHeader("Accept-Language","application/json")
.addHeader("content-type", "application/x-www-form-urlencoded").addHeader("API_KEY", "a5XSE8XCdsY6hAoCNojYBQ")
;
Request request = requestBuilder.build();
return chain.proceed(request);
}
}).build();
public static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.API_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
public static ApiInterface restAPI = retrofit.create(ApiInterface.class);
}
ためのAPI/HTTPクライアント
APIインタフェースクラス
@POST("token")
Call<Token> getToken();
@FormUrlEncoded
@POST("login")
Call<UserAgain> userLogin(@Field("username") String param1, @Field("password") String param2);
@FormUrlEncoded
@POST("logout")
Call<Logout> userLogout(@Field("username") String param1 , @Field("password") String param2);
ログインAPIは、罰金200 OKのレスポンスコードを与える動作します。 https://futurestud.io/tutorials/retrofit-add-custom-request-header API形式:
ユーザー認証/ログイン
目的:ログアウトAPIの追加ダイナミックcustomnヘッダ(クライアントXSRFトークン)
リファレンスを扱うときに大きな問題が発生しました - ユーザーログインの残りURL: -/api/v1/people/login メソッド:-POSTヘッダー:受け入れ言語:application/json API_KEY: a5XSE8XCdsY6hAoCNojYBQコンテンツタイプ:application/x-www-form-urlencoded X-CSRF -token:
ユーザーログアウト
目的: - ユーザログアウト休憩URL: -/API/V1 /人/ログアウト 方法:-POSTヘッダ:言語を受け入れ:アプリケーション/ JSONのAPI_KEYを: a5XSE8XCdsY6hAoCNojYBQのContentタイプ:application/x-www-form-urlencoded X-CSRFトークン:bodyのパラメータ:username:例えば [email protected]パスワード:例123456
私は同じことの参照リンクを使用していたと思うと
セットのOAuthトークン
繰り返し要求-add-custom-request-header – Naman
はい。それはカスタムヘッダーのために私のために働いた。 –
私は使用しているXSRFトークンにいくつかの問題があると思うが、api呼び出しが働かないとbeacuse – Naman