php 使用Googlesheet API创建新的Googlesheet

yc0p9oo0  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(123)
<?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();
}

我正在创建googlesheet与PHP客户端库和谷歌表API的帮助下,但不知道!!这段代码有什么问题,我的新谷歌表甚至没有创建,错误也没有返回。

gfttwv5a

gfttwv5a1#

<?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();
}

相关问题