簡體   English   中英

使用access_token android改造?

[英]Working with access_token android retrofit?

在我的應用程序中,我連接到服務器,並且必須對我的進一步工作獲得成功的響應,在完成此類響應之后,通常我會得到兩個令牌(access + refresh)。 對於我的進一步操作,我必須使用訪問令牌,因為我將無法獲取數據。 通常,此令牌會在30分鍾后過期。 我不明白如何在不失敗應用程序的情況下從服務器獲取新令牌。 我看到了一些問題,我認為使用Retrofit而不修改所有調用的這種OAuth刷新令牌是最好的。 但是我無法理解為我使用它的方式。 現在我正在使用這樣的接口:

public interface APIService {
    @Headers("Content-type: application/json")
    @POST("/v1/login")
    Call<Post> auth(@Body Post body);

    @Headers({"Content-type: application/json",
            "Authorization: Bearer access_token"})
    @GET("/v1/message/list")
    Call<Message> getInMess(@Query("type") int type, @Query("offset") int offset);
}

在那里,我必須手動插入訪問令牌。

然后在MainActivity類中初始化接口:

public void sendPost() 
    {
        final EditText titleEt = findViewById(R.id.login);
        final EditText bodyEt = findViewById(R.id.password);
        final String a = titleEt.getText().toString().trim();
        final String b = bodyEt.getText().toString().trim();
        saveData();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://server/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService mAPIService = retrofit.create(APIService.class);
        //retrofit.create(APIService.class);

        mAPIService.auth(new Post(a, b)).enqueue(new Callback<Post>() {
            @Override
            public void onResponse(@NonNull Call<Post> call, @NonNull Response<Post> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(LoginActivity.this, "Post submitted to API.", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(LoginActivity.this, SecondScreen.class);
                    findViewById(R.id.btn_submit).getBackground().setColorFilter(Color.parseColor("#1cd000"), PorterDuff.Mode.MULTIPLY);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "Unable to submit post to API.Error!!!", Toast.LENGTH_LONG).show();
                    findViewById(R.id.btn_submit).getBackground().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.MULTIPLY);
                }
            }

            @Override
            public void onFailure(@NonNull Call<Post> call, @NonNull Throwable t) {
                Toast.makeText(LoginActivity.this, "Unable to submit post to API.", Toast.LENGTH_LONG).show();

            }
        });
    }

請幫助我了解我進一步發展的策略,因為我無法解決自己的問題。

PS對不起,我的英語不好))

您需要攔截請求並將標頭添加到攔截器中。 我在應用程序中使用它:

public class AuthenticationInterceptor implements Interceptor {


    public AuthenticationInterceptor(Context context) {

    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if(!YOUR_TOKEN.isEmpty()) {
            Request authenticatedRequest = request.newBuilder().header("Authorization", "Bearer:" + YOUR_TOKEN).build();
            return chain.proceed(authenticatedRequest);
        }
        return chain.proceed(request);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM