NicklasWallgren / PokemonGoAPI-PHP

Pokemon Go API PHP library
BSD 2-Clause "Simplified" License
130 stars 51 forks source link

Google Authentication: get access token ? #107

Closed Raphaelbm closed 8 years ago

Raphaelbm commented 8 years ago

Hello !

I try to sign in with my google account.

I can get all informations of the Google Account but i can't get a valide token to connect the user.

Example in Javascript: var profile = googleUser.getBasicProfile(); console.log('ID: ' + profile.getId()); console.log('Name: ' + profile.getName()); console.log('Image URL: ' + profile.getImageUrl()); console.log('Email: ' + profile.getEmail()); var auth = googleUser.getAuthResponse(); console.log("Token: "+auth.id_token);

I don't know how to get the "AUTHENTICATION_REFRESH_TOKEN".

Can I get a good token with the id_token ?

Edit: I will try in PHP.

Raphaelbm commented 8 years ago

I tried with the PHP Google library.

But my token is:

{ "error": "unauthorized_client", "error_description": "Unauthorized" }

My code:

`        $client = new Google_Client();
        $client->setAuthConfigFile('client_secret.json');
        $client->setScopes(array(
             'https://www.googleapis.com/auth/userinfo.email',
             'email',
             'openid',
        ));

        $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/profil/getToken.php');

        $auth_url = $client->createAuthUrl();

        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));`
DrDelay commented 8 years ago

You shouldn't need to write code interacting with any authentication-libraries yourself, PTC and 3 types of Google Auth are all handled within this project.

Take a look at the AuthenticationExample.

TacoBytes commented 8 years ago

@Raphaelbm, @DrDelay is right, you do not have to write any code thanks to @NicklasWallgren and @DrDelay, this has already been done.

When you go through Google and retrieve the authentication code, pass it to the $config->setAuthToken('INSERT_AUTHENTICATION_CODE'). After you do that, there is a refresh token generated, at least I see it when I do a print_r($manager). I am not sure how you would do that in JavaScript.

In PHP, my issue, is how can I get the refreshtoken value from the object shown when I print_r($manager). I know it's protected, but is there a way to get it?

This is what I get when I perform print_r($manager):

NicklasW\PkmGoApi\Authentication\Managers\Google\AuthenticationCodeManager Object ( [authenticationCode:protected] => 4/v1WLDAhFHR4LgVJUHXn-cOuO-12345678901234 [listeners:protected] => Array ( [0] => Closure Object ( [parameter] => Array ( [$event] => [$value] => ) ) ) [accessToken:protected] => NicklasW\PkmGoApi\Authentication\AccessToken Object ( [token:protected] => 'intentionally removed text for this example' [timestamp:protected] => 1471199526 [refreshToken:protected] => 1/b9WjTyO65coDEnQ9S8S054jltTAU0VsaHgnY4WUFGnI [provider:protected] => google ) )

Note: When I hard code the refreshToken shown, I am able to continue without having to request another token.

DrDelay commented 8 years ago

@TacoBytes, look at this example. Persist this $accessToken, it will also contain the refresh token and has a getter for it.

$manager = null;
if ($persistedToken) {
  $manager = Factory::create($persistedToken);
} else {
  $config = new Config();
  $config->setProvider(Factory::PROVIDER_GOOGLE);
  $config->setAuthToken('INSERT_AUTHENTICATION_CODE');
  $manager = Factory::create($config);
}
Raphaelbm commented 8 years ago

Ok thanks you ! It works ! :)

vntopcoders commented 8 years ago

@Raphaelbm Can you share with me your full source code of this feature?