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')); }
}); $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) { $Command->displayHelp(); }
/** * 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);