Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.FileSystems;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5.  
  6. import okhttp3.OkHttpClient;
  7.  
  8. import com.pokegoapi.api.PokemonGo;
  9. import com.pokegoapi.auth.GoogleAuthJson;
  10. import com.pokegoapi.auth.GoogleAuthTokenJson;
  11. import com.pokegoapi.auth.GoogleCredentialProvider;
  12. import com.pokegoapi.auth.GoogleCredentialProvider.OnGoogleLoginOAuthCompleteListener;
  13. import com.pokegoapi.exceptions.LoginFailedException;
  14. import com.pokegoapi.exceptions.RemoteServerException;
  15.  
  16. public class PokeGoHelper implements Runnable {
  17.  
  18.     OkHttpClient httpClient = new OkHttpClient();
  19.     GoogleAuthTokenJson token;
  20.     String refreshToken;
  21.     Path path = FileSystems.getDefault().getPath("", "refreshToken.txt");
  22.    
  23.     static public void main(String[] args) {
  24.         (new Thread(new PokeGoHelper())).start();
  25.     }
  26.    
  27.     public void run() {
  28.         try {
  29.             if (path.toFile().exists())
  30.                 readToken();
  31.         } catch (ClassNotFoundException | IOException e) {
  32.             e.printStackTrace();
  33.         }
  34.  
  35.         if (refreshToken == null) {
  36.             loginPokeGoFirstTime();
  37.         } else {
  38.             loginPokeGo();
  39.         }
  40.     }
  41.  
  42.     private void readToken() throws IOException, ClassNotFoundException {
  43.         refreshToken = Files.readAllLines(path).get(0);
  44.     }
  45.  
  46.     private void saveToken() throws IOException {
  47.         Files.write(path, token.getRefreshToken().getBytes());
  48.     }
  49.  
  50.     private void loginPokeGoFirstTime() {
  51.         try {
  52.             PokemonGo go = new PokemonGo(new GoogleCredentialProvider(
  53.                     httpClient, new OnGoogleLoginOAuthCompleteListener() {
  54.  
  55.                         @Override
  56.                         public void onTokenIdReceived(GoogleAuthTokenJson arg0) {
  57.                             token = arg0;
  58.                             try {
  59.                                 saveToken();
  60.                             } catch (IOException e) {
  61.                                 e.printStackTrace();
  62.                             }
  63.                         }
  64.  
  65.                         @Override
  66.                         public void onInitialOAuthComplete(GoogleAuthJson arg0) {
  67.                         }
  68.                     }), httpClient);
  69.         } catch (LoginFailedException | RemoteServerException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.  
  74.     private void loginPokeGo() {
  75.         try {
  76.             PokemonGo go = new PokemonGo(new GoogleCredentialProvider(
  77.                     httpClient, refreshToken), httpClient);
  78.         } catch (LoginFailedException | RemoteServerException e) {
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement