2017-03-31 13 views
6

Tôi có điều nàyCách tạo bảng tính với google api và đặt quyền thích hợp với PHP?

define('CLIENT_SECRET_PATH', __DIR__ . '/config_api.json'); 
define('ACCESS_TOKEN', '0b502651********c52b3'); 

Tôi có thể tạo bảng tính với điều này và lấy id và url.

$requestBody = new Google_Service_Sheets_Spreadsheet(); 
$response = $service->spreadsheets->create($requestBody); 
print_r($response); 
$new_spr_id = $response['spreadsheetId']; 

Nhưng bảng tính này không xuất hiện trong danh tấm google vì nó là "bảo vệ" hoặc một cái gì đó. Tôi cố gắng để thiết lập cho phép với điều này nhưng nhận được một lỗi: Fatal error: Gọi phương pháp Google_Service_Drive_Permission không xác định :: setValue()

insertPermission($service, $new_spr_id, '**@gmail.com' , 'user', 'owner'); 
function insertPermission($service, $fileId, $value, $type, $role) { 
    $newPermission = new Google_Service_Drive_Permission(); 
    $newPermission->setValue($value); 
    $newPermission->setType($type); 
    $newPermission->setRole($role); 
    try { 
    return $service->permissions->insert($fileId, $newPermission); 
    } catch (Exception $e) { 
    print "An error occurred: " . $e->getMessage(); 
    } 
    return NULL; 
} 

Tôi cần một ví dụ về việc tạo ra một bảng tính mới và thiết lập thích hợp cho phép để tôi có thể sửa đổi bảng tính này từ tài khoản của mình, v.v.

Rất cám ơn nhiều!

+0

là [này] (https://developers.google.com/drive/v3/web/manage-sharing) không đủ? –

Trả lời

2

Tôi nghĩ bạn đang sử dụng API sai. Thiết lập quyền cho các tập tin được tìm thấy trong Drive API permissions.

Nhưng để trả lời câu hỏi của bạn, dưới đây là cách để tạo ra một bảng tính mới sử dụng spreadsheets.create from the Sheets API:

<?php 
/* 
* BEFORE RUNNING: 
* --------------- 
* 1. If not already done, enable the Google Sheets API 
* and check the quota for your project at 
* https://console.developers.google.com/apis/api/sheets 
* 2. Install the PHP client library with Composer. Check installation 
* instructions at https://github.com/google/google-api-php-client. 
*/ 

// Autoload Composer. 
require_once __DIR__ . '/vendor/autoload.php'; 

$client = getClient(); 

$service = new Google_Service_Sheets($client); 

// TODO: Assign values to desired properties of `requestBody`: 
$requestBody = new Google_Service_Sheets_Spreadsheet(); 

$response = $service->spreadsheets->create($requestBody); 

// TODO: Change code below to process the `response` object: 
echo '<pre>', var_export($response, true), '</pre>', "\n"; 

function getClient() { 
    // TODO: Change placeholder below to generate authentication credentials. See 
    // https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample 
    // 
    // Authorize using one of the following scopes: 
    // 'https://www.googleapis.com/auth/drive' 
    // 'https://www.googleapis.com/auth/spreadsheets' 
    return null; 
} 
?> 

Khi các tập tin đã được tạo ra và lưu trong Google Drive của bạn, bây giờ bạn có thể cố gắng thiết lập Quyền sử dụng Drive REST API.

+0

Cảm ơn câu trả lời, nhưng tôi có thể tạo bảng tính mới với $ requestBody = new Google_Service_Sheets_Spreadsheet(); $ response = $ service-> spreadsheets-> create ($ requestBody); Tôi nhận được url và id của bảng tính, nhưng tôi không thể truy cập nó từ tài khoản hoặc api, vì vậy tôi đang tìm một ví dụ về cách thiết lập quyền với API. – SERG

2

Mã của bạn không thể định vị lớp và không thể tạo phiên bản Google_Service_Drive_Permission(). Tôi sẽ đề nghị, không sử dụng chức năng riêng lẻ để tạo đối tượng của Google_Service_Drive_Permission(). Đặt tất cả mã của bạn để đặt quyền trong phần mã, nơi bạn đang tạo tệp. Ngoài ra nếu bạn đang sử dụng nhiều tệp, hãy kiểm tra xem tệp của bạn có đang tải đúng cách và được phân tích cú pháp PHP hay không. Bởi vì Lỗi nghiêm trọng cho cuộc gọi phương thức không xác định không phải là do việc thực hiện các phương thức API, do việc gọi các phương thức không tồn tại hoặc bạn không thể định vị được trình phân tích cú pháp PHP.

Để tham khảo này có thể hữu ích

http://hotexamples.com/examples/-/Google_Service_Drive_Permission/setValue/php-google_service_drive_permission-setvalue-method-examples.html

Các vấn đề liên quan