php - 尝试在 Laravel 中访问 Google Sheets API 时出现“请求缺少有效的 API 密钥”
问题描述
我有一个 Laravel 项目,我想在其上使用 Google Sheets API。我已经创建并仔细检查了 Google API 控制台上生成的所有凭据(API 密钥、OAuth 2.0 客户端 ID 和服务帐户)是否与 .env 文件和 config/google.php 文件的配置相匹配。
config/google.php 文件(请注意 ' ' 里面的值输入正确(这只是为了确保文件的布局正确):
<?php
return [
/*
|----------------------------------------------------------------------------
| Google application name
|----------------------------------------------------------------------------
*/
'application_name' => env('GOOGLE_APPLICATION_NAME', ''),
/*
|----------------------------------------------------------------------------
| Google OAuth 2.0 access
|----------------------------------------------------------------------------
|
| Keys for OAuth 2.0 access, see the API console at
| https://developers.google.com/console
|
*/
'client_id' => env('GOOGLE_CLIENT_ID', ''),
'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
'redirect_uri' => env('GOOGLE_REDIRECT', 'http://127.0.0.1:8000'),
'scopes' => [\Google_Service_Sheets::DRIVE, \Google_Service_Sheets::SPREADSHEETS], // previously only []
'access_type' => 'online',
'approval_prompt' => 'auto',
'prompt' => 'consent',
/*
|----------------------------------------------------------------------------
| Google developer key
|----------------------------------------------------------------------------
|
| Simple API access key, also from the API console. Ensure you get
| a Server key, and not a Browser key.
|
*/
'developer_key' => env('GOOGLE_DEVELOPER_KEY', ''),
/*
|----------------------------------------------------------------------------
| Google service account
|----------------------------------------------------------------------------
|
| Set the credentials JSON's location to use assert credentials, otherwise
| app engine or compute engine will be used.
|
*/
'service' => [
/*
| Enable service account auth or not.
*/
'enable' => env('GOOGLE_SERVICE_ENABLED', true),
/*
| Path to service account json file
*/
'file' => storage_path('app/credentials.json'),
],
/*
|----------------------------------------------------------------------------
| Additional config for the Google Client
|----------------------------------------------------------------------------
|
| Set any additional config variables supported by the Google Client
| Details can be found here:
| https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php
|
| NOTE: If client id is specified here, it will get over written by the one above.
|
*/
'config' => [],
];
我的控制器功能:
public function __invoke(Request $request)
{
$sheets = Sheets::spreadsheet('sheets.THE SHEET ID (cannot disclose)')
->sheetById('sheets.0')
->all();
$posts = array();
foreach ($sheets AS $data) {
$posts[] = array(
'name' => $data[0],
'message' => $data[1],
'created_at' => $data[2],
);
}
return view('apitest')->with(compact('posts'));
}
我的刀片文件:
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', '') }}</title>
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Google Sheets for Laravel Demo</h1>
<div class="row">
<div class="col-sm">
<form action="{{ route('post.store') }}" method="post">
@csrf
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" required>
</div>
<div class="form-group">
<label for="message">Message</label>
<input name="message" type="text" class="form-control" id="message" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="col-sm">
<div class="list-group mt-3">
@foreach($posts as $post)
<div class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ data_get($post, 'name') }}</h5>
</div>
<p class="mb-1">
{{ data_get($post, 'message') }}
</p>
<small>
{{ data_get($post, 'created_at') }} {{ config('app.timezone') }}
</small>
</div>
@endforeach
</div>
</div>
</div>
</div>
<script src="{{ mix('js/app.js') }}"></script>
</body>
但是,当尝试访问该页面时,它总是会给出以下错误,鉴于 API 密钥是正确的,这不太有意义:
解决方案
推荐阅读
- json - 我可以将 Pharo 中的对象导出到文件中吗?
- firebase - 存储在我的 Google Cloud Platform 存储空间中的未知存储分区
- java - 在使用 Java 的 Eclipse IDE 中遵循 HelloWorld 说明时遇到问题
- python - 代码在 Pycharm 上运行,但在控制台中被阻止
- go - 如何通过源代码确定 golang 模块的版本?
- python - pandas - dropna - 如果论点矛盾怎么办?
- sql - SQL Developer 用空格填充 char 中的剩余空间
- python - 如何模块化素数分解 Python 脚本以将列表作为输入?
- ruby - 是否可以在 aws 设备场中实现测试分片?
- javascript - React Js TypeError:无法读取未定义的属性“产品”