php - Grant access on spreadsheet using Google Sheets API
问题描述
I have Google Sheets API set up and working (I can read and update existing spreadsheets if it's shared to my credentials) and I need to export some data from my website to google sheet. In order to keep users off seeing other people's sheets, I need to create a new spreadsheet when user wants to export data. I managed to create new spreadsheet like this:
public function init(){
$user = Socialite::driver('google')->stateless()->user();
$token = [
'access_token' => $user->token,
'refresh_token' => $user->refreshToken,
'expires_in' => $user->expiresIn
];
$client = new \Google_Client();
$client->setApplicationName('Sheets');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig('../credentials.json');
$client->setAccessToken($token);
$service = new Google_Service_Sheets($client);
$serviceDrive = new Google_Service_Drive($client);
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
'properties' => [
'title' => 'Testing Sheet'
]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
'fields' => 'spreadsheetId'
]);
$this->insertPermission($serviceDrive, $spreadsheet->spreadsheetId, $user->email, 'user', 'owner');
}
When I dd($spreadsheet)
I can see that it's actually created and I can retrieve its id. But the thing is, if I try to open it, I get a notification that I need to get access as I don't have it. I searched for a solution a lot and tried several ways. I tried to pass a role like this:
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
'fields' => 'spreadsheetId',
'role' => 'owner'
'email' => 'useremail@gmail.com'
]);
Also tried to insert permission using this method:
function insertPermission($service, $fileId, $value, $type, $role) {
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setEmailAddress($value);
$newPermission->setType($type);
$newPermission->setRole($role);
try {
return $service->permissions->create($fileId, $newPermission);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
But this method gives me an error when calling create()
function which says "code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes."
. Is there a way to give to authenticated user an access to newly created spreadsheet and where is my mistake?
解决方案
- You are trying to give permission with the Drive API
- Your code only contains
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
- To use both the Sheets and the Drive API with the same client, assign to the client BOTH respective scopes
推荐阅读
- java - 旋转矩阵时找不到符号a和b
- python-3.x - 'DatabaseError: DPI-1050: Oracle Client library is at version 0.0 但需要 11.2 或更高版本' 错误显示在远程计算机中
- php - WHERE NOT EXISTS 的正确语法是什么
- github - 如何在 github 拉取请求中显示评论应用程序 url
- flutter - Flutter bloc 状态无法正确更新
- java - 如何将 thymeleaf HTML 页面中的数据发送到 MVC spring boot 控制器?
- html - 如何为每周调度程序 UI 设置跨度元素的样式
- azure - 错误:将 Function 部署到 Azure 后,无法加载文件或程序集“System.ServiceModel,版本 = 4.0.0.0”
- c - GCC 会编译,但 clang 不会
- node.js - NPM 没有显示最新版本的 json-parse-better-errors