Search in sources :

Example 36 with Credentials

use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.

the class TransferOnePidgeyExample method main.

/**
 * Transfers one pidgey from the player's inventory.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http);
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        List<Pokemon> pidgeys = api.inventories.pokebank.getPokemonByPokemonId(PokemonIdOuterClass.PokemonId.PIDGEY);
        if (pidgeys.size() > 0) {
            Pokemon pest = pidgeys.get(0);
            // print the pokemon data
            pest.debug();
            ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result result = pest.transferPokemon();
            Log.i("Main", "Transfered Pidgey result:" + result);
        } else {
            Log.i("Main", "You have no pidgeys :O");
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login. Invalid credentials, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Pokemon(com.pokegoapi.api.pokemon.Pokemon)

Example 37 with Credentials

use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.

the class UseIncenseExample method main.

/**
 * Catches a pokemon at an area.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http, new SystemTimeImpl());
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        api.inventories.itemBag.useIncense();
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) SystemTimeImpl(com.pokegoapi.util.SystemTimeImpl) HashProvider(com.pokegoapi.util.hash.HashProvider)

Example 38 with Credentials

use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.

the class CheckEvolutionExample method main.

/**
 * Displays pokemon evolutions
 *
 * @param args Not used
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        // Login and set location
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        // Get the evolution meta from the item templates received from the game server
        Evolutions evolutionMeta = api.itemTemplates.evolutions;
        System.out.println("Evolutions: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> evolutions = evolutionMeta.getEvolutions(pokemon);
            if (evolutions.size() > 0) {
                System.out.println(pokemon + " -> " + evolutions);
            }
        }
        System.out.println();
        System.out.println("Most basic: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> basic = evolutionMeta.getBasic(pokemon);
            if (basic.size() > 0) {
                // Check this is not the most basic pokemon
                if (!(basic.size() == 1 && basic.contains(pokemon))) {
                    System.out.println(pokemon + " -> " + basic);
                }
            }
        }
        System.out.println();
        System.out.println("Highest: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> highest = evolutionMeta.getHighest(pokemon);
            if (highest.size() > 0) {
                // Check this is not the highest pokemon
                if (!(highest.size() == 1 && highest.contains(pokemon))) {
                    System.out.println(pokemon + " -> " + highest);
                }
            }
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) PokemonId(POGOProtos.Enums.PokemonIdOuterClass.PokemonId) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Evolutions(com.pokegoapi.api.pokemon.Evolutions)

Example 39 with Credentials

use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.

the class TransferMultiplePokemon method main.

/**
 * Transfers all bad pokemon from the player's inventory.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http);
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        PokeBank pokebank = api.inventories.pokebank;
        List<Pokemon> pokemons = pokebank.pokemons;
        List<Pokemon> transferPokemons = new ArrayList<>();
        // Find all pokemon of bad types or with IV less than 25%
        for (Pokemon pokemon : pokemons) {
            PokemonId id = pokemon.getPokemonId();
            double iv = pokemon.getIvInPercentage();
            if (iv < 90) {
                if (id == PokemonId.RATTATA || id == PokemonId.PIDGEY || id == PokemonId.CATERPIE || id == PokemonId.WEEDLE || id == PokemonId.MAGIKARP || id == PokemonId.ZUBAT || iv < 25) {
                    transferPokemons.add(pokemon);
                }
            }
        }
        System.out.println("Releasing " + transferPokemons.size() + " pokemon.");
        Pokemon[] transferArray = transferPokemons.toArray(new Pokemon[transferPokemons.size()]);
        Map<PokemonFamilyId, Integer> responses = pokebank.releasePokemon(transferArray);
        // Loop through all responses and find the total amount of candies earned for each family
        Map<PokemonFamilyId, Integer> candies = new HashMap<>();
        for (Map.Entry<PokemonFamilyId, Integer> entry : responses.entrySet()) {
            int candyAwarded = entry.getValue();
            PokemonFamilyId family = entry.getKey();
            Integer candy = candies.get(family);
            if (candy == null) {
                // candies map does not yet contain the amount if null, so set it to 0
                candy = 0;
            }
            // Add the awarded candies from this request
            candy += candyAwarded;
            candies.put(family, candy);
        }
        for (Map.Entry<PokemonFamilyId, Integer> entry : candies.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + " candies awarded");
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login. Invalid credentials, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) PokeBank(com.pokegoapi.api.inventory.PokeBank) OkHttpClient(okhttp3.OkHttpClient) PokemonId(POGOProtos.Enums.PokemonIdOuterClass.PokemonId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Pokemon(com.pokegoapi.api.pokemon.Pokemon) HashMap(java.util.HashMap) Map(java.util.Map)

Example 40 with Credentials

use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.

the class GoogleCredentialProvider method login.

/**
 * Starts a login flow for google using googles device oauth endpoint.
 *
 * @throws LoginFailedException if an exception occurs while attempting to log in
 * @throws InvalidCredentialsException if invalid credentials are used
 */
public void login() throws LoginFailedException, InvalidCredentialsException {
    HttpUrl url = HttpUrl.parse(OAUTH_ENDPOINT).newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("scope", "openid email https://www.googleapis.com/auth/userinfo.email").build();
    // Create empty body
    RequestBody reqBody = RequestBody.create(null, new byte[0]);
    Request request = new Request.Builder().url(url).method("POST", reqBody).build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new LoginFailedException("Network Request failed to fetch tokenId", e);
    }
    Moshi moshi = new Moshi.Builder().build();
    GoogleAuthJson googleAuth = null;
    try {
        googleAuth = moshi.adapter(GoogleAuthJson.class).fromJson(response.body().string());
        Log.d(TAG, "" + googleAuth.expiresIn);
    } catch (IOException e) {
        throw new LoginFailedException("Failed to unmarshell the Json response to fetch tokenId", e);
    }
    Log.d(TAG, "Get user to go to:" + googleAuth.verificationUrl + " and enter code:" + googleAuth.userCode);
    onGoogleLoginOAuthCompleteListener.onInitialOAuthComplete(googleAuth);
    GoogleAuthTokenJson googleAuthTokenJson;
    try {
        while ((googleAuthTokenJson = poll(googleAuth)) == null) {
            Thread.sleep(googleAuth.interval * 1000);
        }
    } catch (InterruptedException e) {
        throw new LoginFailedException("Sleeping was interrupted", e);
    } catch (IOException e) {
        throw new LoginFailedException(e);
    } catch (URISyntaxException e) {
        throw new LoginFailedException(e);
    }
    Log.d(TAG, "Got token: " + googleAuthTokenJson.idToken);
    onGoogleLoginOAuthCompleteListener.onTokenIdReceived(googleAuthTokenJson);
    expiresTimestamp = System.currentTimeMillis() + (googleAuthTokenJson.expiresIn * 1000 - REFRESH_TOKEN_BUFFER_TIME);
    tokenId = googleAuthTokenJson.idToken;
    refreshToken = googleAuthTokenJson.refreshToken;
}
Also used : Moshi(com.squareup.moshi.Moshi) Request(okhttp3.Request) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) LoginFailedException(com.pokegoapi.exceptions.request.LoginFailedException) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)39 Response (okhttp3.Response)29 OkHttpClient (okhttp3.OkHttpClient)22 IOException (java.io.IOException)20 Test (org.junit.Test)16 HttpUrl (okhttp3.HttpUrl)10 RequestBody (okhttp3.RequestBody)9 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 ResponseBody (okhttp3.ResponseBody)8 NonNull (androidx.annotation.NonNull)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)6 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)6 Observable (rx.Observable)6 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)5 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)5 PokemonGo (com.pokegoapi.api.PokemonGo)5 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)5