我已經設置并運行了 Google Sheets API(如果共享到我的憑據,我可以讀取和更新現有電子表格),并且我需要將一些數據從我的網站導出到 Google Sheet。為了防止用戶看到其他人的工作表,當用戶想要導出數據時,我需要創建一個新的電子表格。我設法創建了這樣的新電子表格: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');}當我dd($spreadsheet)看到它實際上已創建并且我可以檢索它的 id 時。但問題是,如果我嘗試打開它,我會收到一條通知,提示我需要訪問權限,因為我沒有權限。我尋找了很多解決方案并嘗試了多種方法。我試圖傳遞這樣一個角色:$spreadsheet = $service->spreadsheets->create($spreadsheet, [ 'fields' => 'spreadsheetId', 'role' => 'owner' 'email' => '[email protected]']);還嘗試使用此方法插入權限: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; }但是這個方法在調用create()函數時給了我一個錯誤,上面寫著"code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes.". 有沒有辦法讓經過身份驗證的用戶訪問新創建的電子表格,我的錯誤在哪里?
1 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
您正在嘗試使用 Drive API 授予權限
你的代碼只包含
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
要將 Sheets 和 Drive API 與同一客戶端一起使用,請為客戶端分配兩個各自的范圍
- 1 回答
- 0 關注
- 265 瀏覽
添加回答
舉報
0/150
提交
取消