Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.file.FileSystems;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import okhttp3.OkHttpClient;
- import com.pokegoapi.api.PokemonGo;
- import com.pokegoapi.auth.GoogleAuthJson;
- import com.pokegoapi.auth.GoogleAuthTokenJson;
- import com.pokegoapi.auth.GoogleCredentialProvider;
- import com.pokegoapi.auth.GoogleCredentialProvider.OnGoogleLoginOAuthCompleteListener;
- import com.pokegoapi.exceptions.LoginFailedException;
- import com.pokegoapi.exceptions.RemoteServerException;
- public class PokeGoHelper implements Runnable {
- OkHttpClient httpClient = new OkHttpClient();
- GoogleAuthTokenJson token;
- String refreshToken;
- Path path = FileSystems.getDefault().getPath("", "refreshToken.txt");
- static public void main(String[] args) {
- (new Thread(new PokeGoHelper())).start();
- }
- public void run() {
- try {
- if (path.toFile().exists())
- readToken();
- } catch (ClassNotFoundException | IOException e) {
- e.printStackTrace();
- }
- if (refreshToken == null) {
- loginPokeGoFirstTime();
- } else {
- loginPokeGo();
- }
- }
- private void readToken() throws IOException, ClassNotFoundException {
- refreshToken = Files.readAllLines(path).get(0);
- }
- private void saveToken() throws IOException {
- Files.write(path, token.getRefreshToken().getBytes());
- }
- private void loginPokeGoFirstTime() {
- try {
- PokemonGo go = new PokemonGo(new GoogleCredentialProvider(
- httpClient, new OnGoogleLoginOAuthCompleteListener() {
- @Override
- public void onTokenIdReceived(GoogleAuthTokenJson arg0) {
- token = arg0;
- try {
- saveToken();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onInitialOAuthComplete(GoogleAuthJson arg0) {
- }
- }), httpClient);
- } catch (LoginFailedException | RemoteServerException e) {
- e.printStackTrace();
- }
- }
- private void loginPokeGo() {
- try {
- PokemonGo go = new PokemonGo(new GoogleCredentialProvider(
- httpClient, refreshToken), httpClient);
- } catch (LoginFailedException | RemoteServerException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement