2017-03-20 201 views
1

我遇到了Google日曆API的問題。Google Calendar API刷新accessToken

我可以通過authCallback與谷歌的登錄,但一小時後它給了我一個憑證無效的錯誤

應用程序\ HTTP \控制器\ gCalendarController.php

<?php 
namespace App\Http\Controllers; 

use Carbon\Carbon; 
use Google_Client; 
use Google_Service_Calendar; 
use Google_Service_Calendar_Event; 
use Google_Service_Calendar_EventDateTime; 
use Illuminate\Http\Request; 

class gCalendarController extends Controller 
{ 
    protected $client; 

    public function __construct() 
    { 
     $client = new Google_Client(); 
     $client->setAuthConfig('client_secret.json'); 
     $client->addScope(Google_Service_Calendar::CALENDAR); 

     $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false))); 
     $client->setHttpClient($guzzleClient); 
     $this->client = $client; 
    } 

    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     session_start(); 
     if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
      $this->client->setAccessToken($_SESSION['access_token']); 
      $service = new Google_Service_Calendar($this->client); 

      $calendarId = 'primary'; 

      $results = $service->events->listEvents($calendarId); 
      $test = $_SESSION['access_token']; 
      return view('calendar.tyfuscalender', compact('result', 'test')); 

     } else { 
      return redirect()->route('oauthCallback'); 
     } 
    } 

    public function oauth() 
    { 
     session_start(); 

     $rurl = action('[email protected]'); 
     $this->client->setRedirectUri($rurl); 
     if (!isset($_GET['code'])) { 
      $auth_url = $this->client->createAuthUrl(); 
      $filtered_url = filter_var($auth_url, FILTER_SANITIZE_URL); 
      return redirect($filtered_url); 
     } else { 
      $this->client->authenticate($_GET['code']); 
      $_SESSION['access_token'] = $this->client->getAccessToken(); 
      return redirect()->route('cal.index'); 
     } 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     return view('calendar.createEvent'); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(Request $request) 
    { 
     session_start(); 
     $startDateTime = $request->start_date; 
     $endDateTime = $request->end_date; 

     if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
      $this->client->setAccessToken($_SESSION['access_token']); 
      $service = new Google_Service_Calendar($this->client); 

      $calendarId = 'primary'; 
      $event = new Google_Service_Calendar_Event([ 
       'summary' => $request->title, 
       'description' => $request->description, 
       'start' => ['dateTime' => $startDateTime], 
       'end' => ['dateTime' => $endDateTime], 
       'reminders' => ['useDefault' => true], 
      ]); 
      $results = $service->events->insert($calendarId, $event); 
      if (!$results) { 
       return response()->json(['status' => 'error', 'message' => 'Something went wrong']); 
      } 
      return response()->json(['status' => 'success', 'message' => 'Event Created']); 
     } else { 
      return redirect()->route('oauthCallback'); 
     } 
    } 

    /** 
    * Display the specified resource. 
    * 
    * @param $eventId 
    * @return \Illuminate\Http\Response 
    * @internal param int $id 
    */ 
    public function show($eventId) 
    { 
     session_start(); 
     if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
      $this->client->setAccessToken($_SESSION['access_token']); 

      $service = new Google_Service_Calendar($this->client); 
      $event = $service->events->get('primary', $eventId); 

      if (!$event) { 
       return response()->json(['status' => 'error', 'message' => 'Something went wrong']); 
      } 
      return response()->json(['status' => 'success', 'data' => $event]); 

     } else { 
      return redirect()->route('oauthCallback'); 
     } 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit($id) 
    { 
     // 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param $eventId 
    * @return \Illuminate\Http\Response 
    * @internal param int $id 
    */ 
    public function update(Request $request, $eventId) 
    { 
     session_start(); 
     if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
      $this->client->setAccessToken($_SESSION['access_token']); 
      $service = new Google_Service_Calendar($this->client); 

      $startDateTime = Carbon::parse($request->start_date)->toRfc3339String(); 

      $eventDuration = 30; //minutes 

      if ($request->has('end_date')) { 
       $endDateTime = Carbon::parse($request->end_date)->toRfc3339String(); 

      } else { 
       $endDateTime = Carbon::parse($request->start_date)->addMinutes($eventDuration)->toRfc3339String(); 
      } 

      // retrieve the event from the API. 
      $event = $service->events->get('primary', $eventId); 

      $event->setSummary($request->title); 

      $event->setDescription($request->description); 

      //start time 
      $start = new Google_Service_Calendar_EventDateTime(); 
      $start->setDateTime($startDateTime); 
      $event->setStart($start); 

      //end time 
      $end = new Google_Service_Calendar_EventDateTime(); 
      $end->setDateTime($endDateTime); 
      $event->setEnd($end); 

      $updatedEvent = $service->events->update('primary', $event->getId(), $event); 


      if (!$updatedEvent) { 
       return response()->json(['status' => 'error', 'message' => 'Something went wrong']); 
      } 
      return response()->json(['status' => 'success', 'data' => $updatedEvent]); 

     } else { 
      return redirect()->route('oauthCallback'); 
     } 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param $eventId 
    * @return \Illuminate\Http\Response 
    * @internal param int $id 
    */ 
    public function destroy($eventId) 
    { 
     session_start(); 
     if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
      $this->client->setAccessToken($_SESSION['access_token']); 
      $service = new Google_Service_Calendar($this->client); 
      $service->events->delete('primary', $eventId); 
     } else { 
      return redirect()->route('oauthCallback'); 
     } 
    } 
} 

這是錯誤消息

Google_Service_Exception in REST.php line 118: 
{ 
"error": { 
"errors": [ 
{ 
"domain": "global", 
"reason": "authError", 
"message": "Invalid Credentials", 
"locationType": "header", 
"location": "Authorization" 
} 
], 
"code": 401, 
"message": "Invalid Credentials" 
} 
} 

我在想這是與過期的令牌的東西,但我不知道如何s幹掉它吧。

謝謝。

回答

0

我想,如果我回到'/ oauth'路線,它會提示我允許谷歌再次使用我的谷歌帳戶..而不是刷新我的accesstoken ..有沒有辦法讓它刷新?無需返回'/ oauth'。

謝謝