【问题标题】:How to create a spreadsheet with google api and set a proper permissions with PHP?如何使用 google api 创建电子表格并使用 PHP 设置适当的权限?
【发布时间】:2017-03-31 15:47:07
【问题描述】:

我有这个

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

我可以用它创建一个电子表格并获取 id 和 url。

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

但是这个电子表格没有出现在谷歌表格列表中,因为它是“protected”之类的。 我正在尝试使用此设置权限,但出现错误:致命错误:调用未定义的方法 Google_Service_Drive_Permission::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;
}

我需要一个创建新电子表格并为其设置适当权限的示例,以便我可以从我的帐户等修改此电子表格。

非常感谢!

【问题讨论】:

  • this 还不够吗?

标签: php google-spreadsheet-api


【解决方案1】:

您的代码无法找到类并且无法创建 Google_Service_Drive_Permission() 的实例。我建议,不要使用单独的函数来创建 Google_Service_Drive_Permission() 的对象。将所有代码放置在您正在创建文件的代码部分中以设置权限。此外,如果您使用多个文件,请检查您的文件是否正确加载并由 PHP 解析器定位。因为未定义方法调用的致命错误不是由于 API 方法的实现,而是由于调用了不存在的方法或您的 PHP 解析器无法定位。

作为参考,这可能会有所帮助

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

【讨论】:

  • -1 因为 Google_Service_Drive_Permission() 实例已经创建,并且致命错误与该类中缺少名为 setValue 的方法有关。我们可以用setEmailAddress 替换setValue 方法。但是,稍后将针对...->permissions->insert() 触发另一个错误,因为permissions 类型是数组。这是Google API PHP Client 问题,我还没有找到任何解决方法
【解决方案2】:

我遇到了同样的问题,但无法使用专为更改 permissions 而设计的 Google API PHP 客户端方法解决。但是,有可能使用来自PHP Client 的身份验证信息检索 Guzzle 实例。因此,我们可以简单地调用所需的 API 端点来发送请求。下面的代码是在 Google Drive 上更改文件所有者/权限的完整解决方法:

//if you're using Service Account, otherwise follow your normal authorization
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/json');
$client = new Google_Client();
$client->setScopes(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();

//Now the main code to change the file permission begins
$httpClient = $client->authorize(); //It returns a Guzzle instance with proper Headers
$result = $httpClient->request('POST', 'https://www.googleapis.com/drive/v3/files/[FILE_ID]/permissions?transferOwnership=true', [
  'json' => [
    'role' => 'owner',
    'type' => 'user', 
    'emailAddress' => 'email@example.com'
  ]
]);

$result->getBody()->getContents()的示例响应是:

{
 "kind": "drive#permission",
 "id": "14...",
 "type": "user",
 "role": "owner"
}

【讨论】:

    【解决方案3】:

    我认为您使用了错误的 API。文件设置权限见Drive API permissions

    但要回答您的问题,以下是使用 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;
    }
    ?>
    

    在您的 Google Drive 中创建并保存文件后,您现在可以尝试使用 Drive REST API 设置权限。

    【讨论】:

    • 感谢您的回答,但我可以使用 $requestBody = new Google_Service_Sheets_Spreadsheet(); 创建一个新的电子表格$response = $service->电子表格->create($requestBody);我得到了电子表格的 url 和 id,但我无法从帐户或 api 访问它,所以我正在寻找一个有关如何使用 API 设置权限的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2013-10-22
    相关资源
    最近更新 更多