public function testCreateAuthUrl()
    {
        $Client = new WebServerApplication();
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=&access_type=online&approval_prompt=auto
EOD
, $Client->createAuthUrl([]));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=online&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher']));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=online&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], ''));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=online&approval_prompt=auto&state=SOMEstate
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], 'SOMEstate'));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=online&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=online&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_ONLINE));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=auto
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_AUTO));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=force
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE));
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=force&login_hint=my_hint
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE, 'my_hint'));
        $Client->setClientId('ClieNTID');
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=ClieNTID&redirect_uri=&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=force&login_hint=my_hint
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE, 'my_hint'));
        $Client->setRedirectUri('http:/example.com/oauth2callback');
        $this->assertEquals(<<<'EOD'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=ClieNTID&redirect_uri=http%3A%2Fexample.com%2Foauth2callback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&access_type=offline&approval_prompt=force&login_hint=my_hint
EOD
, $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE, 'my_hint'));
    }
Exemplo n.º 2
0
$Command->appendParameter(new Option('client', 'c', 'client id', Option::TYPE_STRING, true), function ($name, $value) use(&$clientId) {
    $clientId = $value;
});
$Command->appendParameter(new Option('redirect', 'r', 'redirect uri', Option::TYPE_STRING, true), function ($name, $value) use(&$redirectUri) {
    $redirectUri = $value;
});
$Command->appendParameter(new Option('scopes', 's', 'grant scopes', Option::TYPE_STRING, true), function ($name, $value) use(&$scopes) {
    $scopes = explode(',', $value);
});
$Command->appendParameter(new Option('secret', 'e', 'client secret', Option::TYPE_STRING, true), function ($name, $value) use(&$clientSecret) {
    $clientSecret = $value;
});
try {
    $Command->parse(true);
    // Create new client
    $Client = new WebServerApplication();
    $Client->setClientId($clientId)->setClientSecret($clientSecret)->setRedirectUri($redirectUri);
    if (!empty($code)) {
        // Get authorization token for access application
        $Token = $Client->authorizeByCode($code);
        if ($Token instanceof Token) {
            printf("%s\n", (string) $Token);
        } else {
            var_dump($Token);
        }
    } else {
        // Get authorization uri
        $uri = $Client->createAuthUrl($scopes, '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE);
        printf("%s\n", $uri);
    }
} catch (RequiredOptionException $Ex) {
Exemplo n.º 3
0
/**
 * Copyright 2015 Alexey Maslov <*****@*****.**>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Google web server authorization uri 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', REDIRECT_URI = 'http://example.com/oauth2callback';
// Create new client
$Client = new WebServerApplication();
$Client->setClientId(CLIENT_ID)->setClientSecret(CLIENT_SECRET)->setRedirectUri(REDIRECT_URI);
// Create authorization url
$url = $Client->createAuthUrl(['https://www.googleapis.com/auth/androidpublisher'], '', WebServerApplication::RESPONSE_TYPE_CODE, WebServerApplication::ACCESS_TYPE_OFFLINE, WebServerApplication::APPROVAL_PROMPT_FORCE);
var_dump($url);
Exemplo n.º 4
0
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 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 {
Exemplo n.º 5
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();
}
Exemplo n.º 6
0
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Script for Google API access token refreshing
 * @author alxmsl
 */
include __DIR__ . '/../vendor/autoload.php';
use alxmsl\Cli\CommandPosix;
use alxmsl\Cli\Exception\RequiredOptionException;
use alxmsl\Cli\Option;
use alxmsl\Google\OAuth2\WebServerApplication;
$token = '';
$Command = new CommandPosix();
$Command->appendHelpParameter('show help');
$Command->appendParameter(new Option('token', 't', 'revoked token', Option::TYPE_STRING, true), function ($name, $value) use(&$token) {
    $token = $value;
});
try {
    $Command->parse(true);
    $Client = new WebServerApplication();
    $revoked = $Client->revoke($token);
    if ($revoked) {
        printf("token %s revoked\n", $token);
    } else {
        printf("token %s was not revoke\n", $token);
    }
} catch (RequiredOptionException $Ex) {
    $Command->displayHelp();
}