尝试获取非对象laravel-google-sheets的属性“ access_token”

时间:2019-06-09 23:00:36

标签: laravel request google-sheets-api

晚安,我正在按照本教程使用带有laravel的google api表

https://github.com/kawax/laravel-google-sheets

当我尝试做第一个例子时

use Sheets;
$user = $request->user();
$token = [
  'access_token'  => $user->access_token,
  'refresh_token' => $user->refresh_token,
  'expires_in'    => $user->expires_in,
  'created'       => $user->updated_at->getTimestamp(),
];

// all() returns array
$values = Sheets::setAccessToken($token)->spreadsheet('spreadsheetId')->sheet('Sheet 1')->all();

我的代码:

namespace App\Http\Controllers;
use Sheets;
use Google;
class PlanilhaController extends Controller
{
public function index(Request $request)
{
    $user = $request->user();
     $token = [
               'access_token'  => $user->access_token,
               'refresh_token'=> $user->refresh_token,
               'expires_in'=> $user->expires_in,
               'created' => $user->updated_at->getTimestamp(),
     ];
  $values = Sheets::setAccessToken($token) 
  >spreadsheet('spreadsheetId')->sheet('Sheet 1')->all();
   // all() returns array
    return view('planilha', compact('values'));
}

错误:尝试获取非对象的属性'access_token'

没有要求,但我不知道如何解决

1 个答案:

答案 0 :(得分:0)

实际上,您不需要该令牌,只需设置.env,使其具有后继密钥

GOOGLE_APPLICATION_NAME=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT=

GOOGLE_DEVELOPER_KEY=
GOOGLE_SERVICE_ENABLED=
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=

POST_SPREADSHEET_ID=
POST_SHEET_ID=

然后在您的控制器中

$sheets = Sheets::spreadsheet(config('sheets.post_spreadsheet_id'))
                        ->sheet(config('sheets.post_sheet_id'))
                        ->get();
        $header = $sheets->pull(0);
        $posts = Sheets::collection($header, $sheets);
        $posts = $posts->reverse()->take(10);

然后在您的配置文件中写入

'post_spreadsheet_id' => env('POST_SPREADSHEET_ID'),
'post_sheet_id'       => env('POST_SHEET_ID'),