NicklasWallgren / PokemonGoAPI-PHP

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

How can i integrate Gmail API to login? #132

Closed vntopcoders closed 8 years ago

vntopcoders commented 8 years ago

Can you guide me how to login with Google APIs

DrDelay commented 8 years ago

I did not test it, but it should give you a start:

use NicklasW\PkmGoApi\Authentication\Config\Config;
use NicklasW\PkmGoApi\Authentication\Factory\Factory;
use NicklasW\PkmGoApi\Authentication\Manager;
use NicklasW\PkmGoApi\Kernels\ApplicationKernel;

// (...)

/** @var \Psr\Cache\CacheItemPoolInterface $cache */
/** @var \Psr\Http\Message\ServerRequestInterface $request */
/** @var \Psr\Http\Message\ResponseInterface $response */

// Create the authentication config either from cache or POST. Redirect user to get authcode else
$config = null;
$tokenCache = $cache->getItem(ACCESS_TOKEN_CACHEKEY);
if ($request->getMethod() == 'POST' && isset($request->getParsedBody()[AUTHCODE_POST_NAME])) {
    $config = new Config();
    $config->setProvider(Factory::PROVIDER_GOOGLE);
    $config->setAuthToken($request->getParsedBody()[AUTHCODE_POST_NAME]);
} elseif ($tokenCache->isHit()) {
    $config = $tokenCache->get();
} else {
    return $response->withHeader('Location', 'https://accounts.google.com/o/oauth2/auth?client_id=848232511240-73ri3t7plvk96pj4f85uj8otdat2alem.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=openid%20email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&approval_prompt=force');
}

$manager = Factory::create($config);

// Add a event listener to persist the token
$manager->addListener(function ($event, $value) use ($cache, $tokenCache) {
    if ($event === Manager::EVENT_ACCESS_TOKEN) {
        /** @var \NicklasW\PkmGoApi\Authentication\AccessToken $value */
        $cache->save($tokenCache
            ->set($value)
            ->expiresAt(\DateTime::createFromFormat('U', $value->getTimestamp())));
    }
});

$application = new ApplicationKernel($manager);
vntopcoders commented 8 years ago

Thank you for reply! I try many times but I got the error: NicklasW\PkmGoApi\Handlers\RequestHandler\Exceptions\AuthenticationException Object ( [message:protected] => Invalid authentication token provided [string:Exception:private] => [code:protected] => 0 [file:protected] => /usr/share/nginx/html/poke/src/Handlers/RequestHandler.php [line:protected] => 179 [trace:Exception:private] => Array ( [0] => Array ( [file] => /usr/share/nginx/html/poke/src/Handlers/RequestHandler.php [line] => 125 [function] => validateResponseEnvelope [class] => NicklasW\PkmGoApi\Handlers\RequestHandler [type] => -> [args] => Array ( [0] => POGOProtos\Networking\Envelopes\ResponseEnvelope Object ( [_unknown:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => [statusCode:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => 102 [requestId:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => 0 [apiUrl:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => [unknown6:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => Array ( ) [authTicket:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => [returns:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => Array ( ) [error:POGOProtos\Networking\Envelopes\ResponseEnvelope:private] => [_unknown:ProtobufMessage:private] => ) ) ) [1] => Array ( [file] => /usr/share/nginx/html/poke/src/Services/Request/InventoryRequestService.php [line] => 24 [function] => handle [class] => NicklasW\PkmGoApi\Handlers\RequestHandler [type] => -> [args] => Array ( [0] => NicklasW\PkmGoApi\Requests\GetInventoryRequest Object ( [type:protected] => 4 [message:protected] => [data:protected] => ) ) ) [2] => Array ( [file] => /usr/share/nginx/html/poke/src/Api/Player/Inventory.php [line] => 59 [function] => getInventory [class] => NicklasW\PkmGoApi\Services\Request\InventoryRequestService [type] => -> [args] => Array ( ) ) [3] => Array ( [file] => /usr/share/nginx/html/poke/src/Api/Procedure.php [line] => 50 [function] => update [class] => NicklasW\PkmGoApi\Api\Player\Inventory [type] => -> [args] => Array ( ) ) [4] => Array ( [file] => /usr/share/nginx/html/poke/overview.php [line] => 51 [function] => __call [class] => NicklasW\PkmGoApi\Api\Procedure [type] => -> [args] => Array ( [0] => getPokeBank [1] => Array ( ) ) ) [5] => Array ( [file] => /usr/share/nginx/html/poke/overview.php [line] => 51 [function] => getPokeBank [class] => NicklasW\PkmGoApi\Api\Player\Inventory [type] => -> [args] => Array ( ) ) ) [previous:Exception:private] => )