<?php
require_once __DIR__ . '/vendor/autoload.php';
$client_id = '*********.apps.googleusercontent.com';
$client_secret = '**************************';
$redirect_uri = 'https://{site}/wp-admin/plugins.php/oauth';
$client = new Google_Client();
$client->setClientId( $client_id );
$client->setClientSecret( $client_secret );
$client->setRedirectUri( $redirect_uri );
$client->setScopes( array('https://www.googleapis.com/auth/drive') );
if ( isset( $_GET['code'] ) ) {
$client->authenticate( $_GET['code'] );
$_SESSION['access_token'] = $client->getAccessToken();
header( 'Location: ' . filter_var( $redirect_uri, FILTER_SANITIZE_URL ) );
exit;
}
if ( ! isset( $_SESSION['access_token'] ) ) {
$auth_url = $client->createAuthUrl();
header( 'Location: ' . filter_var( $auth_url, FILTER_SANITIZE_URL ) );
exit;
}
$client->setAccessToken( $_SESSION['access_token'] );
$service = new Google_Service_Sheets($client);
$spreadsheet = new Google_Service_Sheets_Spreadsheet(array(
'properties' => array(
'title' => 'My New Spreadsheet'
),
'sheets' => array(
new Google_Service_Sheets_Sheet(array(
'properties' => array(
'title' => 'Sheet1',
'gridProperties' => array(
'rowCount' => 20,
'columnCount' => 12
)
)
))
)
));
$spreadsheet = $service->spreadsheets->create($spreadsheet, array('fields' => 'spreadsheetId'));
// Print the new spreadsheet's ID
echo 'Spreadsheet ID: ' . $spreadsheet->getSpreadsheetId();
}
I was creating googlesheet with help of php client library and google sheet API but Don't know !! What is wrong with this code my new google sheet is not even created and the error is also not returned.
What is wrong with this code my new google sheet is not even created and the error is also not returned.
, in your script, it seems that when a new Spreadsheet is created, the spreadsheet ID of the created Spreadsheet is shown. How about this? nothing is returning even the sheet id
, in this case, I think that a new Spreadsheet is not created. But, when I tested your script using my access token, the spreadsheet ID is returned and I can confirm the created Spreadsheet. So, how about confirming your access token again? <?php
require_once __DIR__ . '/vendor/autoload.php';
$client_id = '*********.apps.googleusercontent.com';
$client_secret = '**************************';
$redirect_uri = 'https://{site}/wp-admin/plugins.php/oauth';
$client = new Google_Client();
$client->setClientId( $client_id );
$client->setClientSecret( $client_secret );
$client->setRedirectUri( $redirect_uri );
$client->setScopes( array('https://www.googleapis.com/auth/drive') );
if ( isset( $_GET['code'] ) ) {
$client->authenticate( $_GET['code'] );
$_SESSION['access_token'] = $client->getAccessToken();
header( 'Location: ' . filter_var( $redirect_uri, FILTER_SANITIZE_URL ) );
exit;
}
if ( ! isset( $_SESSION['access_token'] ) ) {
$auth_url = $client->createAuthUrl();
header( 'Location: ' . filter_var( $auth_url, FILTER_SANITIZE_URL ) );
exit;
}
$client->setAccessToken( $_SESSION['access_token'] );
$service = new Google_Service_Sheets($client);
$spreadsheet = new Google_Service_Sheets_Spreadsheet(array(
'properties' => array(
'title' => 'My New Spreadsheet'
),
'sheets' => array(
new Google_Service_Sheets_Sheet(array(
'properties' => array(
'title' => 'Sheet1',
'gridProperties' => array(
'rowCount' => 20,
'columnCount' => 12
)
)
))
)
));
$spreadsheet = $service->spreadsheets->create($spreadsheet, array('fields' => 'spreadsheetId'));
// Print the new spreadsheet's ID
echo 'Spreadsheet ID: ' . $spreadsheet->getSpreadsheetId();
}