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

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