本文整理汇总了PHP中Google_Client::setAccessToken方法的典型用法代码示例。如果您正苦于以下问题:PHP Google_Client::setAccessToken方法的具体用法?PHP Google_Client::setAccessToken怎么用?PHP Google_Client::setAccessToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Google_Client
的用法示例。
在下文中一共展示了Google_Client::setAccessToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: google
function google()
{
$client = new Google_Client();
$client->setApplicationName("snmmaurya");
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setApprovalPrompt(APPROVAL_PROMPT);
$client->setAccessType(ACCESS_TYPE);
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['error'])) {
echo '<script type="text/javascript">window.close();</script>';
exit;
}
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get();
$_SESSION['User'] = $user;
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
}
}
示例2: getGoogleApiClient
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
private function getGoogleApiClient()
{
$client = new Google_Client();
$client->setApplicationName($this->CFG['GOOGLE_API_APPLICATION_NAME']);
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfigFile($this->CFG['GOOGLE_API_CLIENT_SECRET_PATH']);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = $this->CFG['GOOGLE_API_CREDENTIALS_PATH'];
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
示例3: setTokenFromStorage
/**
* Retrieves an access token from the storage object and sets it into the
* client object.
*/
public function setTokenFromStorage()
{
$accessToken = $this->storage->get();
if (isset($accessToken)) {
$this->client->setAccessToken($accessToken);
}
}
示例4: __construct
/**
* @param ContainerInterface $container
* @param TranslatorInterface $translator
* @param LoggerInterface $logger
*
* @throws InvalidConfigurationException
*/
public function __construct(ContainerInterface $container, TranslatorInterface $translator, LoggerInterface $logger)
{
$this->container = $container;
// Check if we have the API key
$rootDir = $this->container->getParameter('kernel.root_dir');
$configDir = $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;
$apiKeyFile = $configDir . $this->container->getParameter('dms.service_account_key_file');
if (!file_exists($apiKeyFile)) {
throw new InvalidConfigurationException('Store your Google API key in ' . $apiKeyFile . ' - see https://code.google.com/apis/console');
}
// Perform API authentication
$apiKeyFileContents = file_get_contents($apiKeyFile);
$serviceAccountEmail = $this->container->getParameter('dms.service_account_email');
$auth = new \Google_Auth_AssertionCredentials($serviceAccountEmail, array('https://www.googleapis.com/auth/drive'), $apiKeyFileContents);
$this->client = new \Google_Client();
if (isset($_SESSION['service_token'])) {
$this->client->setAccessToken($_SESSION['service_token']);
}
$this->client->setAssertionCredentials($auth);
/*
if ($this->client->getAuth()->isAccessTokenExpired()) {
$this->client->getAuth()->refreshTokenWithAssertion($auth);
}
*/
$this->translator = $translator;
$this->logger = $logger;
$this->service = new \Google_Service_Drive($this->client);
}
示例5: testSettersGetters
public function testSettersGetters()
{
$client = new Google_Client();
$client->setClientId("client1");
$client->setClientSecret('client1secret');
$client->setState('1');
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
global $apiConfig;
$this->assertEquals('client1', $apiConfig['oauth2_client_id']);
$this->assertEquals('client1secret', $apiConfig['oauth2_client_secret']);
$client->setRedirectUri('localhost');
$client->setApplicationName('me');
$client->setUseObjects(false);
$this->assertEquals('object', gettype($client->getAuth()));
$this->assertEquals('object', gettype($client->getCache()));
$this->assertEquals('object', gettype($client->getIo()));
$client->setAuthClass('Google_AuthNone');
$client->setAuthClass('Google_OAuth2');
try {
$client->setAccessToken(null);
die('Should have thrown an Google_AuthException.');
} catch (Google_AuthException $e) {
$this->assertEquals('Could not json decode the token', $e->getMessage());
}
$token = json_encode(array('access_token' => 'token'));
$client->setAccessToken($token);
$this->assertEquals($token, $client->getAccessToken());
}
示例6: refreshToken
public function refreshToken($accessToken)
{
$this->_googleClient->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($this->_googleClient->isAccessTokenExpired()) {
$this->_googleClient->refreshToken($this->_googleClient->getRefreshToken());
}
return $this->_googleClient->getAccessToken();
}
示例7: shorten
public function shorten($longUrl)
{
$this->_googleClient->setAccessToken(json_encode(["access_token" => 'ya29.Ci-nA63j_-YJy7-M4u0F490pttfSg-Gka2xLyDzaqmfWgDCMXLVsisYGoGLmqT55Vg']));
$service = new \Google_Service_Urlshortener($this->_googleClient);
$url = new \Google_Service_Urlshortener_Url();
$url->longUrl = $longUrl;
print_r($service->url->insert($url));
die;
return $service->url->insert($url);
}
示例8: setAccessToken
/**
* @param string $accessToken The token
*/
public function setAccessToken($accessToken)
{
if (is_null($accessToken)) {
$this->token = null;
} else {
$token = array('access_token' => $accessToken);
$this->token = json_encode($token);
}
$this->client->setAccessToken($this->token);
}
示例9: isLoggedIn
/**
* @return string
*/
public function isLoggedIn()
{
if (\Session::has('token')) {
$this->client->setAccessToken(\Session::get('token'));
} else {
return false;
}
if ($this->client->isAccessTokenExpired()) {
\Session::set('token', $this->client->getRefreshToken());
}
return !$this->client->isAccessTokenExpired();
}
示例10: __construct
public function __construct()
{
parent::__construct();
if (!BaseTest::$client) {
global $apiConfig;
$apiConfig['ioFileCache_directory'] = '/tmp/google-api-php-client/tests';
BaseTest::$client = new Google_Client();
if (!BaseTest::$client->getAccessToken()) {
BaseTest::$client->setAccessToken($apiConfig['oauth_test_token']);
}
}
}
示例11: cmdLineAuth
/**
* Auth over command line
*/
public function cmdLineAuth()
{
$authUrl = $this->client->createAuthUrl();
//Request authorization
print "Please visit:\n{$authUrl}\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $this->client->authenticate($authCode);
$this->client->setAccessToken($accessToken);
$this->accessToken = $accessToken;
$this->refreshToken = $this->client->getRefreshToken();
}
示例12: getClient
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
global $credentialsPath;
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(GAPI_SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
die("Please, get token via connect.php");
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client = refreshToken($client);
}
return $client;
}
示例13: __construct
/**
* Sets the required params for authentication
*
* @param string $client_id our app's client id
* @param string $client_secret our app's client secret
* @param string $redirect_uri where will the auth server send the client
* after generating an authorization code to authenticate
* @param null|string $token token obtained when a code is authenticated
* @param null|string $code response code to authenticate
* @param null|array $scopes array of strings with OAuth2/google scopes
* to requested
* @param null|Google_Client $client
* @param bool $debug defaults to false
*/
public function __construct($client_id, $client_secret, $redirect_uri, $token = null, $code = null, $scopes = null, $client = null, $debug = null)
{
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->redirect_uri = $redirect_uri;
$this->code = $code;
$this->token = $token;
if ($scopes !== null) {
$this->scopes = $scopes;
}
if (is_bool($this->debug)) {
$this->debug = $debug;
} else {
$this->debug = false;
}
if ($client === null) {
$this->client = new Google_Client();
$this->client->setClientId($this->client_id);
$this->client->setClientSecret($this->client_secret);
$this->client->setRedirectUri($this->redirect_uri);
if ($token !== null) {
$this->client->setAccessToken($token);
}
$this->client->setScopes($this->scopes);
}
}
示例14: call_back
public function call_back()
{
$config = new Controllers_Api_Google_Config_App();
$client = new Google_Client();
$client->setClientId($config->config['client_id']);
$client->setClientSecret($config->config['client_secret']);
$client->setRedirectUri($config->config['redirect_uri']);
$client->addScope("email");
$client->addScope("profile");
$service = new Google_Service_Oauth2($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . filter_var($config->config['redirect_uri'], FILTER_SANITIZE_URL));
exit;
}
/************************************************
If we have an access token, we can make
requests, else we generate an authentication URL.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
if (isset($authUrl)) {
//show login url
echo json_encode(array('status' => false, 'data' => $authUrl));
} else {
$user = $service->userinfo->get();
//get user info
echo json_encode(array('status' => true, 'data' => $user));
}
}
示例15: authorizeGoogleUser
function authorizeGoogleUser($access_code)
{
$client = new \Google_Client();
$google = $this->config->google;
$client->setApplicationName('Portal da Rede');
$client->setClientId($google->clientId);
$client->setClientSecret($google->secret);
$client->setRedirectUri('postmessage');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->authenticate($access_code);
$json_token = $client->getAccessToken();
$client->setAccessToken($json_token);
$plus = new \Google_Service_Plus($client);
$user = $plus->people->get('me');
if (!$user->emails || !is_array($user->emails)) {
return;
}
$email = $user->emails[0]['value'];
$user_email = $this->db->user_email->find_one($email);
if (!$user_email) {
return;
}
$this->login($user_email->user);
}