如何在改造中获取访问令牌

时间:2018-01-25 15:38:19

标签: java android get retrofit

我正在创建一个注册应用程序,我已经获得了客户端ID和秘密,现在我必须获取请求并获取访问令牌和刷新令牌。我有这个链接https://supptop.mymix.net/api/token?grant_type=password&client_id=yourclientID&client_secret=yourclientSecret&email=email@gmail.com&password=password'

这是界面。

public interface SupportopApi {

@GET("/api/token")
Call<ResponseBody> getToken(@Query("grant_type") String grant_type,
                            @Query("client_id") String client_id,
                            @Query("client_secret") String client_secret,
                            @Query("email") String email,
                            @Query("password") String password);}

好的,现在这是下一堂课。

public class ApiClient {

private static ApiClient instance;

private SupportopApi supportopApi;

private ApiClient(String endpoint) {

    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS);

    clientBuilder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder()
                    .addHeader("Content-Type", "application/json")
                    .build();
            return chain.proceed(request);
        }
    });

    supportopApi = new Retrofit.Builder()
            .baseUrl(endpoint)
            .client(clientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(SupportopApi.class);
}

  public Call<ResponseBody> gettoken(String grant_type, String client_id, String client_secret,
                                   String email, String password){

    return supportopApi.getToken(grant_type, client_id, client_secret, email, password);}

好的,这是我打电话的课程。

public class FragmentRegistration extends Fragment {
View mainView;

EditText username, email, password, name;
Button button;

ApiClient pentairAPIClient = ApiClient.getInstance();

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mainView = inflater.inflate
            (R.layout.registration, container, false);

    username = mainView.findViewById(R.id.username);
    email = mainView.findViewById(R.id.email);
    password = mainView.findViewById(R.id.password);
    name = mainView.findViewById(R.id.name);
    button = mainView.findViewById(R.id.register);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s = name.getText().toString();
            String split[] = s.split(" ");

            updateApp();
        }
    });

    return mainView;
}

public void updateApp() {
    FragmentRegistration context = this;

    Call<ResponseBody> call = pentairAPIClient.registration(supportopObj);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                activationCall();

            } else {
                Toast.makeText(getContext(), "Something went wrong",
                        Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getContext(), "Error...", Toast.LENGTH_SHORT).show();
        }
    });
}


public void activationCall() {
    Call<ResponseBody> callActive = pentairAPIClient.activation(supportopObjActivate);
    callActive.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {


            if (response.isSuccessful()) {

                try {
                    String data = response.body().string();
                    JSONObject obj = new JSONObject(data);
                    String client_id = obj.getString("client_id");
                    String client_secret = obj.getString("client_secret");


                    Call<ResponseBody> getToken = pentairAPIClient.gettoken("password",client_id, client_secret,
                            email.getText().toString(), password.getText().toString());

                    getToken.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                           if(response.isSuccessful()){
                               Toast.makeText(getContext(), String.valueOf(response.body()), Toast.LENGTH_SHORT).show();
                           }
                           else Toast.makeText(getContext(), "Something wrong", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            Toast.makeText(getContext(), "You're on failure", Toast.LENGTH_SHORT).show();
                        }
                    });


                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }

            } else
                try {
                    Toast.makeText(getContext(), response.body().string(),
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getContext(), "Error in activation",
                    Toast.LENGTH_SHORT).show();
        }
    });
}}

我不知道我必须在哪里粘贴链接,我将在其中包含我的client_id client_secret,邮件,密码。

1 个答案:

答案 0 :(得分:0)

尝试使用@Header

https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor

我认为client_id和client_secret是Headers,grant_type,email,password是参数。

例如

public class LoginBody {
     private String email;
     private String password;
}

Call<String> call = service.request(AdminTokenUtils.getAuthorizationWeb(),body);

Call<String> request(
            @Header("Authorization") String token,
            @Body LoginBody body
    );