Search in sources :

Example 1 with InstagramAuthException

use of me.postaddict.instagram.scraper.exception.InstagramAuthException in project instagram-java-scraper by postaddictme.

the class Instagram method login.

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).add("queryParams", "{}").add("optIntoOneTap", "true").build();
    Request request = new Request.Builder().url(Endpoint.LOGIN_URL).header(Endpoint.REFERER, Endpoint.BASE_URL + "/accounts/login/").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", ErrorType.UNAUTHORIZED);
        }
    }
}
Also used : Response(okhttp3.Response) ActionResponse(me.postaddict.instagram.scraper.model.ActionResponse) InstagramAuthException(me.postaddict.instagram.scraper.exception.InstagramAuthException) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) FormBody(okhttp3.FormBody) GetMediaByTagRequest(me.postaddict.instagram.scraper.request.GetMediaByTagRequest) GetLocationRequest(me.postaddict.instagram.scraper.request.GetLocationRequest) GetMediasRequest(me.postaddict.instagram.scraper.request.GetMediasRequest) GetMediaLikesRequest(me.postaddict.instagram.scraper.request.GetMediaLikesRequest) Request(okhttp3.Request) GetFollowsRequest(me.postaddict.instagram.scraper.request.GetFollowsRequest) GetFollowersRequest(me.postaddict.instagram.scraper.request.GetFollowersRequest) RequestBody(okhttp3.RequestBody)

Example 2 with InstagramAuthException

use of me.postaddict.instagram.scraper.exception.InstagramAuthException in project instagram-java-scraper by postaddictme.

the class ErrorInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response response = chain.proceed(chain.request());
    int code = response.code();
    if (code == 200) {
        return response;
    } else {
        String bodyString = String.valueOf(response.body().string());
        response.body().close();
        ErrorType errorType = ErrorType.UNKNOWN_ERROR;
        switch(code) {
            case 400:
                if (bodyString.contains("Sorry, you're following the max limit of accounts.")) {
                    errorType = ErrorType.FOLLOWING_THE_MAX_LIMIT_OF_ACCOUNTS;
                } else if (bodyString.contains("feedback_required") || bodyString.contains("Action Blocked") || bodyString.contains("\\u30d6\\u30ed\\u30c3\\u30af\\u3055\\u308c\\u3066\\u3044\\u307e\\u3059")) {
                    errorType = ErrorType.ACTION_BLOCKED;
                } else if (bodyString.contains("checkpoint_required")) {
                    errorType = ErrorType.CHECKPOINT_REQUIRED;
                } else if (bodyString.contains("two_factor_required")) {
                    errorType = ErrorType.TWO_FACTOR_REQUIRED;
                }
                throw new InstagramException("Bad Request", errorType);
            case 401:
                throw new InstagramAuthException("Unauthorized", ErrorType.UNAUTHORIZED);
            case 403:
                if (bodyString.contains("Please wait a few minutes before you try again.") || bodyString.contains("数分してからもう一度実行してください。")) {
                    errorType = ErrorType.TEMPORARY_ACTION_BLOCKED;
                } else if (bodyString.contains("unauthorized")) {
                    errorType = ErrorType.UNAUTHORIZED;
                }
                throw new InstagramAuthException("Access denied", errorType);
            case 404:
                throw new InstagramNotFoundException("Resource does not exist", errorType);
            case 429:
                throw new InstagramException("Rate limited", ErrorType.RATE_LIMITED);
            case 502:
                throw new InstagramException("Bad Gateway", ErrorType.INSTAGRAM_SERVER_ERROR);
            default:
                throw new InstagramException("Response code is " + code + ".", errorType);
        }
    }
}
Also used : Response(okhttp3.Response) InstagramAuthException(me.postaddict.instagram.scraper.exception.InstagramAuthException) ErrorType(me.postaddict.instagram.scraper.ErrorType) InstagramException(me.postaddict.instagram.scraper.exception.InstagramException) InstagramNotFoundException(me.postaddict.instagram.scraper.exception.InstagramNotFoundException)

Aggregations

InstagramAuthException (me.postaddict.instagram.scraper.exception.InstagramAuthException)2 Response (okhttp3.Response)2 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 ErrorType (me.postaddict.instagram.scraper.ErrorType)1 InstagramException (me.postaddict.instagram.scraper.exception.InstagramException)1 InstagramNotFoundException (me.postaddict.instagram.scraper.exception.InstagramNotFoundException)1 ActionResponse (me.postaddict.instagram.scraper.model.ActionResponse)1 GetFollowersRequest (me.postaddict.instagram.scraper.request.GetFollowersRequest)1 GetFollowsRequest (me.postaddict.instagram.scraper.request.GetFollowsRequest)1 GetLocationRequest (me.postaddict.instagram.scraper.request.GetLocationRequest)1 GetMediaByTagRequest (me.postaddict.instagram.scraper.request.GetMediaByTagRequest)1 GetMediaLikesRequest (me.postaddict.instagram.scraper.request.GetMediaLikesRequest)1 GetMediasRequest (me.postaddict.instagram.scraper.request.GetMediasRequest)1 FormBody (okhttp3.FormBody)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1