@Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); int code = response.code(); if (code == 200) { return response; } else { response.body().close(); } switch (code) { case 401: throw new InstagramAuthException("Unauthorized"); case 403: throw new InstagramAuthException("Access denied"); case 404: throw new InstagramNotFoundException("Resource does not exist"); default: throw new InstagramException("Response code is not equal 200. Something went wrong. Please report issue."); } } }
public void login(String username, String password) throws IOException { if (username == null || password == null) { throw new InstagramAuthException("Specify username and password"); }else if(this.csrf_token.isEmpty()) { throw new NullPointerException("Please run before base()"); } RequestBody formBody = new FormBody.Builder() .add("username", username) .add("password", password) .build(); Request request = new Request.Builder() .url(Endpoint.LOGIN_URL) .header(Endpoint.REFERER, Endpoint.BASE_URL + "/") .post(formBody) .build(); Response response = executeHttpRequest(withCsrfToken(request)); try(InputStream jsonStream = response.body().byteStream()) { if(!mapper.isAuthenticated(jsonStream)){ throw new InstagramAuthException("Credentials rejected by instagram"); } } }
@Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); int code = response.code(); if (code == 200) { return response; } else { response.body().close(); } switch (code) { case 401: throw new InstagramAuthException("Unauthorized"); case 403: throw new InstagramAuthException("Access denied"); case 404: throw new InstagramNotFoundException("Resource does not exist"); default: throw new InstagramException("Response code is not equal 200. Something went wrong. Please report issue."); } } }
public void login(String username, String password) throws IOException { if (username == null || password == null) { throw new InstagramAuthException("Specify username and password"); }else if(this.csrf_token.isEmpty()) { throw new NullPointerException("Please run before base()"); } RequestBody formBody = new FormBody.Builder() .add("username", username) .add("password", password) .build(); Request request = new Request.Builder() .url(Endpoint.LOGIN_URL) .header(Endpoint.REFERER, Endpoint.BASE_URL + "/") .post(formBody) .build(); Response response = executeHttpRequest(withCsrfToken(request)); try(InputStream jsonStream = response.body().byteStream()) { if(!mapper.isAuthenticated(jsonStream)){ throw new InstagramAuthException("Credentials rejected by instagram"); } } }