<?php

function customGuzzleClient( $googleClient ) {
    $options = [
        'base_uri' => $googleClient->getConfig( 'base_path' ),
        'exceptions' => false,
        'verify' => false,
    ];

    return new GuzzleHttp\Client( $options );
}

// session_start();

require_once 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

$client = new Google_Client();

$client->setHttpClient( customGuzzleClient( $client ) );

$client->setAuthConfig( 'client_secrets.json' );
$client->setRedirectUri(
    'http://' . $_SERVER[ 'HTTP_HOST' ] . '/saintx.php'
);

$client->addScope( Google_Service_Analytics::ANALYTICS_READONLY );

if ( empty( $_GET[ 'code' ] ) ) {
    header( 'Location: ' . $client->createAuthUrl(), true, 301 );
} else {
    $client->authenticate( $_GET[ 'code' ] );

    $_SESSION[ 'access_token' ] = $client->getAccessToken();

    var_dump( $client->getAccessToken(), $_SESSION );

    // header( 'http://' . $_SERVER[ 'HTTP_HOST' ], true, 301 );
}
bunu dene