Php 需要日历API登录

Php 需要日历API登录,php,laravel,api,calendar,Php,Laravel,Api,Calendar,我尝试添加谷歌日历API,作为回应,我得到了这个错误 Google_Service_Exception in REST.php line 118: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "mess

我尝试添加谷歌日历API,作为回应,我得到了这个错误

 Google_Service_Exception in REST.php line 118:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
App/Providers/GoogleClientServiceProvider.php
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class GoogleClientServiceProvider extends ServiceProvider {

    function register(){
        /* Get config variables */
        $service_account_name = getenv('GOOGLE_SERVICE_ACCOUNT_NAME');
        $key_file_location = base_path() . getenv('GOOGLE_KEY_FILE_LOCATION');

        $key = file_get_contents($key_file_location);
        /* Add the scopes you need */
        $scopes = ['https://www.googleapis.com/auth/calendar'];

        $client = new \Google_Client();
        $this->cal = new Google_Service_Calendar($client);

        $cred = new \Google_Auth_AssertionCredentials(
            $service_account_name,
            $scopes,
            $key
        );


        $client->setAssertionCredentials($cred);

        // repeat this block for as many Google_Services_* as you like
        // don't forget to add scopes for other services.
        $this->app->singleton(\Google_Service_Calendar::class, function ($app) use ($client) {
            return new \Google_Service_Calendar($client);
        });
    }

}
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Google_Service_Calendar;
use App\kosten;
use App\User;

class AgendaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Google_Service_Calendar $client){
      $calendarList = $client->calendarList->listCalendarList();
      dd($calendarList->getItems());
    }  

    // public function index()
    // {


    //   return view('agenda');
    // }
}
App/http/Controllers/AgendaController
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class GoogleClientServiceProvider extends ServiceProvider {

    function register(){
        /* Get config variables */
        $service_account_name = getenv('GOOGLE_SERVICE_ACCOUNT_NAME');
        $key_file_location = base_path() . getenv('GOOGLE_KEY_FILE_LOCATION');

        $key = file_get_contents($key_file_location);
        /* Add the scopes you need */
        $scopes = ['https://www.googleapis.com/auth/calendar'];

        $client = new \Google_Client();
        $this->cal = new Google_Service_Calendar($client);

        $cred = new \Google_Auth_AssertionCredentials(
            $service_account_name,
            $scopes,
            $key
        );


        $client->setAssertionCredentials($cred);

        // repeat this block for as many Google_Services_* as you like
        // don't forget to add scopes for other services.
        $this->app->singleton(\Google_Service_Calendar::class, function ($app) use ($client) {
            return new \Google_Service_Calendar($client);
        });
    }

}
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Google_Service_Calendar;
use App\kosten;
use App\User;

class AgendaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Google_Service_Calendar $client){
      $calendarList = $client->calendarList->listCalendarList();
      dd($calendarList->getItems());
    }  

    // public function index()
    // {


    //   return view('agenda');
    // }
}
谷歌服务账户名称=谷歌服务账户
GOOGLE\u KEY\u FILE\u LOCATION=/resources/assets/KEY.p12

您可以尝试以下代码:

 putenv('GOOGLE_APPLICATION_CREDENTIALS=' .get_include_path().'/yourPath/client_secret.json');
    define('SCOPES', implode(' ', array(
            Google_Service_Calendar::CALENDAR)
    ));

    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->setScopes(SCOPES);
    $client->setApplicationName("MasterSinge");

    $service = new Google_Service_Calendar($client);

    $calendarList = $service->calendarList->listCalendarList();

你有最新的谷歌Api日历吗?我如何检查我是否有最新的版本?我的同事下载了it@MarioEekmaGoogle Calendar API v2已被弃用,我建议您下载以下内容:检查您的Google_应用程序_凭据的路径是否正确。您可以创建另一个服务帐户并下载JSON。