Search code examples
phpcomposer-phplumengoogle-api-php-client

Getting the Google Client to work in Lumen


I am trying to get the google api client for php to work in Lumen, however trying to create a client results in my api to give a 'Oops, something went wrong' error with no further useful information.

I think it is something to do with the way im trying to import the google api client into Lumen, i used composer to install it. But i am not sure if i am using the right thing for Lumen. If i change my require_once it would state that it could not find the 'autoload.php' in the current path. I've also tried the composer update -vvv

The Google folder is present in my vendor folder, and the composer.json also has a entry for the google/apiclient

    "require": {
        "php": ">=7.1.3",
        "google/apiclient": "2.0",
        "laravel/lumen-framework": "5.8.*"
    },
<?php

namespace App\Http\Controllers;

require_once dirname(__DIR__).'\..\..\vendor\autoload.php';

use App\Author;
use Illuminate\Http\Request;

class AccessController extends Controller
{
    private function getToken()
    {
        $credentialsFilePath = 'service_account.json';
        $client = new Google_Client();
        $client->setAuthConfig($credentialsFilePath);

        $client->addScope('https://googleapis.com/auth/analytics.readonly');
        $client->setApplicationName("GoogleAnalytics");
        $client->refreshTokenWithAssertion();
        $token = $client->getAccessToken();
        $accessToken = $token['access_token'];

        return $accessToken;
    }

    public function showAccess()
    {
        $at = getToken();

        return response('Token: ');
    }


}

As you can see im trying to get the service account access token from a json that is saved at the server (no problems). but whenever the line $client = new Google_Client(); is called, i get the 'woops, something went wrong' error from Lumen


Solution

  • It appears i forgot the use Google_Client; Pretty simple but something i completely overlooked.