Get credentials access token programmatically from login URL
Hello,
How to get the access token without using the allow button in browser and copy/paste manually the token ? The authentification working well for me, i'm just looking for to make it full automatic, the user have just to clic only one button when he opens the app.
This is my code :
GoogleUserCredentialProvider provider = new GoogleUserCredentialProvider(httpClient);
// in this url, you will get a code for the google account that is logged
System.out.println("Please go to " + GoogleUserCredentialProvider.LOGIN_URL);
System.out.println("Enter authorisation code:");
// Ask the user to enter it in the standart input
Scanner sc = new Scanner(System.in);
String access = sc.nextLine();
// we should be able to login with this token
provider.login(access);
/**
* After this, if you do not want to re-authorize the google account every time,
* you will need to store the refresh_token that you can get the first time with provider.getRefreshToken()
* ! The API does not store the refresh token for you !
* log in using the refresh token like this :
*/
PokemonGo go = new PokemonGo(new GoogleUserCredentialProvider(httpClient, provider.getRefreshToken(), new PokeTime()), httpClient);
Thanks.
You can use the Googleautocredenialprovider however it will cause the other devices your users use to have to keep reauthorizing thier 2FA
This provider need username and password, i don't want the user to input his credentials, i think this is possible because it's and Android app so it knows about the user Google account.
@meher19 the other way itsn´t possible, because its not the offical pokemon go app.
(For outside android) You can't and you will never can outside of android since you need to get an access_token for the pogo clientID/clientSecret and they disabled all other way to get a token except the google play services api and the token based login (at least for now).
(For android) If you are on android, i dont know well how this can work but from my knowledge, the android oauth flow is only enabled under a package name, you should read the official android doc for this.
this shouldn´t work as well https://developers.google.com/android/guides/http-auth i think.
but maybe it could work. i don´t see any api key related stuff in:
static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
private void pickUserAccount() {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
and
String mEmail; // Received from newChooseAccountIntent(); passed to getToken()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
// Receiving a result from the AccountPicker
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
// With the account name acquired, go get the auth token
getUsername();
} else if (resultCode == RESULT_CANCELED) {
// The account picker dialog closed without selecting an account.
// Notify users that they must pick an account to proceed.
Toast.makeText(this, R.string.pick_account, Toast.LENGTH_SHORT).show();
}
}
// Handle the result from exceptions
...
}
i'm sure it's possible, i have an app accessing to PokemonGo this way, i think FabianTerhorst is in the right way, i will try this !
the access_token should be the same like the from from the other methods.
The method description of getToken says that you need the oauth2 api enabled in the dev console, but maybe it could still work.
in the example you given where i can use getToken method ?
public class GetUsernameTask extends AsyncTask<void, void,="" void=""> {
Activity mActivity;
String mScope;
String mEmail;
GetUsernameTask(Activity activity, String name, String scope) {
this.mActivity = activity;
this.mScope = scope;
this.mEmail = name;
}
/**
* Executes the asynchronous job. This runs when you call execute()
* on the AsyncTask instance.
*/
@Override
protected Void doInBackground(Void... params) {
try {
String token = fetchToken();
if (token != null) {
// **Insert the good stuff here.**
// Use the token to access the user's Google data.
...
}
} catch (IOException e) {
// The fetchToken() method handles Google-specific exceptions,
// so this indicates something went wrong at a higher level.
// TIP: Check for network connectivity before starting the AsyncTask.
...
}
return null;
}
/**
* Gets an authentication token from Google and handles any
* GoogleAuthException that may occur.
*/
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
mActivity.handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
...
}
return null;
}
...
}
String mEmail; // Received from newChooseAccountIntent(); passed to getToken()
String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";
/**
* Attempts to retrieve the username.
* If the account is not yet known, invoke the picker. Once the account is known,
* start an instance of the AsyncTask to get the auth token and do work with it.
*/
private void getUsername() {
if (mEmail == null) {
pickUserAccount();
} else {
if (isDeviceOnline()) {
new GetUsernameTask(HelloActivity.this, mEmail, SCOPE).execute();
} else {
Toast.makeText(this, R.string.not_online, Toast.LENGTH_LONG).show();
}
}
}
Thank you, i will try this !
Does it worked without Dev Console?