Reputation: 110
i am trying to get files to google drive
i figure out to use example in to get list of file with with "ID clients OAuth 2.0"
once i take the "tokenID" it only work once and i guess that's how it's suppose to work,
i want use the API drive to get files only on my account
so i am going use in Backend server
however there's other type of API authorization for server in console.developers.google and i guess that will be helpful for my idea but don't know how to use it
here's other example i talk about using token
new update on the code i set access offline Note: used cookie just for example and testing
the new problem work for the first time then when i refresh again it just show code without any result or error
<?php
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
include_once "templates/base.php";
session_start();
$client_id = '';
$client_secret = '';
$redirect_uri = '{link}';
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client = new Google_Client();
//4/N8jf5YPn4-BwY2J5v30gL5aM-78z2IL7QZMN_TV0odk#
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setDeveloperKey("{Api server code here}");
if(isset($_COOKIE['code'])||isset($_GET['code']))
{
if(isset($_GET['code'])) {
setcookie('code',$_GET['code']);
echo"Saved code";
}
echo $_COOKIE['code']."<br/>";
$refreshToken=$_COOKIE['code'];
$client->authenticate($refreshToken);
if($client->isAccessTokenExpired()) {
$client->refreshToken($refreshToken);
setcookie('code',$client->getAccessToken());
echo "New token: ".$_COOKIE['code']."<br/>";
}
$service = new Google_Service_Drive($client);
var_dump(retrieveAllFiles($service));
} else
{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>get code</a>";
}
Upvotes: 2
Views: 574
Reputation: 110
i figure out my problem -i didn't notice that my php configuration didn't show error -i was confuse between code you get in redirect link and "access token" i thought was the same
here's code
<?php
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
include_once "templates/base.php";
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
$client_id = '';
$client_secret = '';
$redirect_uri = '';
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/drive");
if(isset($_GET['code']))
{
$code=$_GET['code'];
$client->authenticate($code);
$_SESSION['access_token'] = $client->getAccessToken();
}
if(isset($_SESSION['access_token']))
{
echo " token";
var_dump($_SESSION['access_token']);
$client->setAccessToken($_SESSION['access_token']);
if($client->isAccessTokenExpired()) {
$client->refreshToken(json_decode($_SESSION['access_token'])->refresh_token);
$_SESSION['access_token']=$client->getAccessToken();
echo "new token";
var_dump($_SESSION['access_token']);
}
$service = new Google_Service_Drive($client);
var_dump(retrieveAllFiles($service));
} else
{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>get code</a>";
}
Upvotes: 0