* See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Google web server authorization example
 * @author alxmsl
 * @date 1/24/13
 */
include '../vendor/autoload.php';
use alxmsl\Google\OAuth2\Response\Token;
use alxmsl\Google\OAuth2\WebServerApplication;
// Define client identification
const CLIENT_ID = 'my-client@id', CLIENT_SECRET = 'clientsecret';
// Create new client
$Client = new WebServerApplication();
$Client->setClientId(CLIENT_ID)->setClientSecret(CLIENT_SECRET);
// Get access token
$Token = $Client->authorizeByCode($code);
if ($Token instanceof Token) {
    var_dump($Token);
    // Get refresh token
    $Refreshed = $Client->refresh($Token->getRefreshToken());
    var_dump($Refreshed);
    $revoked = $Client->revoke($Token->getAccessToken());
    if ($revoked) {
        printf('token %s was revoke', $Token->getAccessToken());
    } else {
        printf('error on revoke token %s', $Token->getAccessToken());
    }
} else {
    var_dump($Token);
}
Exemple #2
0
 */
include __DIR__ . '/../vendor/autoload.php';
use alxmsl\Cli\CommandPosix;
use alxmsl\Cli\Exception\RequiredOptionException;
use alxmsl\Cli\Option;
use alxmsl\Google\OAuth2\WebServerApplication;
$clientId = '';
$clientSecret = '';
$refreshToken = '';
$Command = new CommandPosix();
$Command->appendHelpParameter('show help');
$Command->appendParameter(new Option('client', 'c', 'client id', Option::TYPE_STRING, true), function ($name, $value) use(&$clientId) {
    $clientId = $value;
});
$Command->appendParameter(new Option('secret', 'e', 'client secret', Option::TYPE_STRING, true), function ($name, $value) use(&$clientSecret) {
    $clientSecret = $value;
});
$Command->appendParameter(new Option('token', 't', 'refresh token', Option::TYPE_STRING, true), function ($name, $value) use(&$refreshToken) {
    $refreshToken = $value;
});
try {
    $Command->parse(true);
    // Create new client
    $Client = new WebServerApplication();
    $Client->setClientId($clientId)->setClientSecret($clientSecret);
    // Get refreshed authorization token
    $Token = $Client->refresh($refreshToken);
    printf("%s\n", (string) $Token);
} catch (RequiredOptionException $Ex) {
    $Command->displayHelp();
}