Google图纸API v4创建图纸并邀请用户 [英] Google sheets API v4 Create sheet and invite users

查看:158
本文介绍了Google图纸API v4创建图纸并邀请用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用服务帐户身份验证功能,使用Google Sheets API创建Google表格。我想创建一个电子表格,并以某种方式允许我的团队打开它。

  $ private_key = file_get_contents($ rootDir。'/ config /googleApiCredentials.p12' ); 
$ client_email ='project-names@positive-water-134xxx.iam.gserviceaccount.com';
$ scopes = implode('',[https://www.googleapis.com/auth/drive]);

$ credentials = new \Google_Auth_AssertionCredentials(
$ client_email,
$ scopes,
$ private_key
);
//用额外的构造函数参数尝试一次:
// $ privateKeyPassword ='notasecret',
// $ assertionType ='http://oauth.net/grant_type/jwt/1.0/ bearer',
// $ sub ='myMail@gmail.com

$ this-> googleClient = new \Google_Client();
$ this-> googleClient-> setAssertionCredentials($ credentials);
if($ this-> googleClient-> getAuth() - > isAccessTokenExpired()){
$ this-> googleClient-> getAuth() - > refreshTokenWithAssertion();
}
$ service = new \Google_Service_Sheets($ this-> googleClient);
$ newSheet = new \Google_Service_Sheets_Spreadsheet();
$ newSheet-> setSpreadsheetId('34534534'); //< - 测试硬编码
$ response = $ service-> spreadsheets-> create($ newSheet);
print_r($ response);

工作表正在创建中,我收到了响应

  [spreadsheetId] => 1opxZmOf5dEPSLoKD1miMgwJRn643cXip6yCquPYxxx 

但是,我无法通过浏览器打开此文件,使用我的谷歌账户,作为Google开发者控制台中的所有者,编辑,作者和读者在项目权限中添加。浏览器说我很无聊,我可以联系管理员获取权限



有什么建议吗?有没有人管理过如何创建文档并为任何人类授予访问权限? 解决方案

服务帐户是一个虚拟用户。它拥有自己的驱动器帐户,拥有它在其帐户中创建的文件。该服务帐户需要让您的个人谷歌驱动器帐户访问该文件。 (与其他用户分享)



注意:您目前已经通过身份验证,可以使用Google工作表API来将Google Drive API(范围)添加到Google开发者控制台上的代码和项目中。无法通过Google图纸API更改文件的权限。



是的,您必须为您希望允许查看/编辑此文件的每个团队成员添加权限。



提示:当您添加Google Drive API时,请尝试执行 files.list 在结果中查找downloadUrl,alternateLink和embedLink。有时填写这些内容,可用于仅将视图分享给其他用户。


I am using a service account authentication to create a google sheet using the Google Sheets API. I want to create a spreadsheet and somehow allow my team to open it.

    $private_key  = file_get_contents($rootDir . '/config/googleApiCredentials.p12');
    $client_email = 'project-names@positive-water-134xxx.iam.gserviceaccount.com';
    $scopes       = implode(' ', ["https://www.googleapis.com/auth/drive"]);

    $credentials = new \Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key
    );
    // tried once with additional constructor params:
    // $privateKeyPassword = 'notasecret',
    // $assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
    // $sub = 'myMail@gmail.com

    $this->googleClient = new \Google_Client();
    $this->googleClient->setAssertionCredentials($credentials);
    if ($this->googleClient->getAuth()->isAccessTokenExpired()) {
        $this->googleClient->getAuth()->refreshTokenWithAssertion();
    }
    $service = new \Google_Service_Sheets($this->googleClient);
    $newSheet = new \Google_Service_Sheets_Spreadsheet();
    $newSheet->setSpreadsheetId('34534534'); // <- hardcoded for test
    $response = $service->spreadsheets->create($newSheet);
    print_r($response);

The sheet is beeing created, I receive response with

[spreadsheetId] => 1opxZmOf5dEPSLoKD1miMgwJRn643cXip6yCquPYxxx

However, I can't open this file through browser, using my google's account, even when Im added as the owner, editor, writer and reader in Google's developer console, in project's permissions. Browser says that I'm unathorized and I can contact administrator for permissions

Any suggestions? Has anyone managed how to create documents and give access to it for any "human"?

解决方案

A service account is a dummy user. It has its own drive account it owns the file it created on its account. The service account will need to give your personal google drive account access to said file. (Share it with you just like any other user)

Permissions: insert Inserts a permission for a file.

Note: You are currently authenticated to use the Google sheets API you are going to have to add google drive API (scope) to your code and your project on Google developers console. There is no way to change the permissions on a file via the google sheets API.

Yes you are going to have to add permissions for every member of your team who you want to allow to see / edit this file.

Tip: When you add google drive API try and do a files.list look for downloadUrl, alternateLink and embedLink in the results. Sometimes these are filled out and can be used for sharing viewable only to another user.

这篇关于Google图纸API v4创建图纸并邀请用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆