简体   繁体   English

带有身份验证令牌的Rest API

[英]Rest API with authentication token

I have an android application calling a Rest API with the authentication token, with the following code 我有一个使用身份验证令牌调用Rest API的android应用,代码如下

private void apiCall(){
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams param = new RequestParams();
    client.addHeader("IDENTITY_KEY",TOKEN);
    client.get(URL, param, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
             mTextMessage.setText(statusCode);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            mTextMessage.setText(statusCode+"");
        }
    });
}

the problem is when I run the application it returns 0, WHY? 问题是当我运行应用程序时,它返回0,为什么? URL and TOKEN declared and initialized outside the method. 在方法外声明和初始化的URL和TOKEN。

I would recommend you to use the retrofit library to do that. 我建议您使用改装库来执行此操作。

Let's say your URL base is http://baseurl.com/api and you have to perform a GET request to /login passing the email and password. 假设您的网址库为http://baseurl.com/api,并且您必须通过传递电子邮件和密码来执行对/ login的GET请求。 I am assuming that your API will return a User object as JSON. 我假设您的API将以JSON形式返回User对象。

Api.java Api.java

@GET("login")
Call<LoginResponse> verifyLogin(@Query("email") String email, @Query("password") String password);

Where you need to perform API call do the following 在需要执行API调用的地方执行以下操作

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://baseurl.com/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
Api api = retrofit.create(Api.class);
Call<LoginResponse> responseCall = api.verifyLogin("email","password");
    responseCall.enqueue(new Callback<ProductResponse>() {
        @Override
        public void onResponse(Call<ProductResponse> call, Response<ProductResponse> response) {
            if (response.isSuccessful()){
                //do whatever you need, 
            }
        }

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

            Toast.makeText(context, "There was an error.", Toast.LENGTH_SHORT).show();
            Log.e("error",t.getMessage().toString());

        }
    });

Note: LoginResponse is a POJO class. 注意:LoginResponse是POJO类。 You have to make a POJO class to perform retrofit actions. 您必须制作一个POJO类来执行改装操作。 It's pretty easy. 很简单 You can learn more about Retrofit from here doc 1 doc 2 您可以从此处了解有关改造的更多信息doc 1 doc 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSAL (Java) Rest API 身份验证(授权:不记名令牌) - MSAL (Java) Rest API Authentication ( Authorization : Bearer Token ) 如何在Servlet上创建REST API以使用访问令牌进行身份验证 - How to create REST API on Servlet for authentication with access token 使用令牌保护REST API - Securing REST api with Token 用于 Jira REST API 的令牌 - Token for Jira REST API Swift和REST API身份验证 - Swift and REST API authentication REST API 使用 SAML 进行身份验证 - REST API authentication with SAML Spring Boot:当令牌也来自调用另一个 api 时,rest api 中请求标头中的令牌身份验证(承载) - Spring Boot : Token authentication(bearer) in request headers in rest api when token also comes from calling another api 如何使用uid密钥和私有密钥来检索令牌来实现Java Rest-Assured API身份验证? - How to implement Java Rest-Assured API authentication using the uid key and secret key to retrieve token? 使用 Spring 引导 Rest API 使用过期令牌进行 Cron 作业调度程序和身份验证 - Cron job scheduler and authentication with expiring token using Spring Boot Rest API 除了基于令牌的身份验证之外,还允许Rest api端点具有http basic auth - In addition to token based authentication, allow Rest api endpoint with http basic auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM