Google PHPクライアントを使用してスプレッドシートデータにアクセスしています。Google Api Phpクライアント - スプレッドシート権限エラー
私は、この致命的なエラーを取得:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }
は私のコード:
$client = new Google_Client();
$client->setApplicationName("Google spreadsheets");
$client->setDeveloperKey("xxxxx");
$client->setScopes(array('https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive.file'));
$service = new Google_Service_Sheets($client);
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($sheetid, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
print "Name, Major:\n";
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s\n", $row[0], $row[4]);
}
}
この問題を解決する方法は?
スプレッドシートは私に所有されています。これは、プロジェクトにアクセスしているプロジェクトの所有者を意味します。また、oAuth認証なしでアクセスしたいと思います。 –
Googleデベロッパーコンソールのプロジェクトでは、所有者のデータを所有している開発者であっても、そのユーザーのデータにアクセスすることはできません。まだプライベートユーザデータにアクセスするための許可が必要です。あなたは、サービスアカウントを使用して調べる必要があります。シートにアクセスできるように事前にサービスアカウントを設定することができます。これを確認してくださいhttps://developers.google.com/api-client-library/php/auth/service-accounts – DaImTo
サービスアカウントを作成し、ダウンロードしたjsonファイルを使用しました。私はコンソールでサービスアカウントをどこで承認するのかはわかりません。このリンクでは、gsuiteの承認について語っています。あなたは指導できますか? –