Skip to content

Class GClient

Stefan Kientzler edited this page Apr 26, 2023 · 3 revisions

Full name:     \SKien\Google\GClient


Class to connect to the google API using OAuth2 authentication.

This class only usese cURL.

Best practice is to use the OAuth client JSON configuration file, which can be downloaded from Google Cloud Console, to set all project and customer specific information (IDs, secrets, URIs).

Create a client configuration at https://console.cloud.google.com

See Also:

Constants

Constant Description
GET GET request
POST POST request
PUT PUT request
PATCH PATCH request
DELETE DELETE request

Overview

Method Description
__construct
addScope Add scope that is neede for following API requests.
buildAuthURL Build the URL to call the google OAuth2.
fetchJsonResponse Cleint request to the API.
fetchTokens Send request to get access and refresh token from a passed auth code.
getAccessToken The current set access token.
getAuthHeader Get the OAuth HTTP header for API requests.
getAuthURI Getter for the current auth URI.
getClientID Getter for the current client ID.
getClientSecret Getter for the current client secret.
getLastError Error text if the last API request has failed.
getLastResponseCode Response code of the last API request.
getLastStatus Status if the last API request has failed.
getProjectID Getter for the current project ID.
getRedirectURI Getter for the current redirect URI.
getRefreshToken Getter for the current refresh token.
getTokenURI Getter for the current token URI.
isAccessTokenExpired Check, if the actual set access token has expired.
parseHttpHeader Parse the header of an HTTP response.
refreshAccessToken Refresh expired access token.
setAccessToken Set a saved access token.
setAuthURI Set the current auth URI.
setClientID Set the current client ID.
setClientSecret Set the current client secret.
setError Set information about the last error occured.
setLogger Set a logger instance.
setOAuthClient Set the OAuth2 client configuration from the google API console.
setProjectID Set the current project ID.
setRedirectURI Set the current redirect URI.
setTokenURI Set the current token URI.

Methods

__construct

public GClient::__construct(\Psr\Log\LoggerInterface $oLogger = null)

Parameters:

Parameter Type Description
oLogger \Psr\Log\LoggerInterface

[go to top]


addScope

Add scope that is neede for following API requests.

public GClient::addScope(string|string[] $scope) : void

Parameters:

Parameter Type Description
scope string | string[]

[go to top]


buildAuthURL

Build the URL to call the google OAuth2.

public GClient::buildAuthURL(string $strLoginHint = '') : string

Description for the $strLoginHint param from google docs:

When your application knows which user it is trying to authenticate, it may provide this parameter as a hint to the Authentication Server. Passing this hint will either pre-fill the email box on the sign-in form or select the proper multi-login session, thereby simplifying the login flow.

Parameters:

Parameter Type Description
strLoginHint string an existing google account to preselect in the login form.

Return Type: string

[go to top]


fetchJsonResponse

Cleint request to the API.

public GClient::fetchJsonResponse(string $strURI, int $iMethod, string[] $aHeader = [], string $data = '') : string|false

Parameters:

Parameter Type Description
strURI string
iMethod int
aHeader string[]
data string

Return Type: string|false

[go to top]


fetchTokens

Send request to get access and refresh token from a passed auth code.

public GClient::fetchTokens(string $strAuthCode) : bool

Parameters:

Parameter Type Description
strAuthCode string the code passed from accounts.google.com

Return Type: bool

[go to top]


getAccessToken

The current set access token.

public GClient::getAccessToken() : array

Return Type: array

[go to top]


getAuthHeader

Get the OAuth HTTP header for API requests.

public GClient::getAuthHeader() : string

Return Type: string

[go to top]


getAuthURI

Getter for the current auth URI.

public GClient::getAuthURI() : string

Return Type: string

[go to top]


getClientID

Getter for the current client ID.

public GClient::getClientID() : string

Return Type: string

[go to top]


getClientSecret

Getter for the current client secret.

public GClient::getClientSecret() : string

Return Type: string

[go to top]


getLastError

Error text if the last API request has failed.

public GClient::getLastError() : string

Return Type: string

[go to top]


getLastResponseCode

Response code of the last API request.

public GClient::getLastResponseCode() : int

Return Type: int

[go to top]


getLastStatus

Status if the last API request has failed.

public GClient::getLastStatus() : string

Return Type: string

[go to top]


getProjectID

Getter for the current project ID.

public GClient::getProjectID() : string

Return Type: string

[go to top]


getRedirectURI

Getter for the current redirect URI.

public GClient::getRedirectURI() : string

Return Type: string

[go to top]


getRefreshToken

Getter for the current refresh token.

public GClient::getRefreshToken() : string

Return Type: string

[go to top]


getTokenURI

Getter for the current token URI.

public GClient::getTokenURI() : string

Return Type: string

[go to top]


isAccessTokenExpired

Check, if the actual set access token has expired.

public GClient::isAccessTokenExpired(int $iOffset = 20) : bool

It is recommended to set an offset to give time for the execution of the next request.

Parameters:

Parameter Type Description
iOffset int additional offset until 'real' expiration

Return Type: bool

[go to top]


parseHttpHeader

Parse the header of an HTTP response.

public GClient::parseHttpHeader(string $strHeader) : array<string,string>

Parameters:

Parameter Type Description
strHeader string

Return Type: array<string,string>

[go to top]


refreshAccessToken

Refresh expired access token.

public GClient::refreshAccessToken(string $strRefreshToken) : array

Parameters:

Parameter Type Description
strRefreshToken string

Return Type: array

new access token

[go to top]


setAccessToken

Set a saved access token.

public GClient::setAccessToken(string|array $token) : void

Parameters:

Parameter Type Description
token string | array accesstoken as string (JSON) or array

[go to top]


setAuthURI

Set the current auth URI.

public GClient::setAuthURI(string $strAuthURI) : void

Parameters:

Parameter Type Description
strAuthURI string

[go to top]


setClientID

Set the current client ID.

public GClient::setClientID(string $strClientID) : void

Parameters:

Parameter Type Description
strClientID string

[go to top]


setClientSecret

Set the current client secret.

public GClient::setClientSecret(string $strClientSecret) : void

Parameters:

Parameter Type Description
strClientSecret string

[go to top]


setError

Set information about the last error occured.

public GClient::setError(int $iResponseCode, string $strError, string $strStatus) : void

If any error can be detected before an API request is made, use this method to set an reproduceable errormessage.

Parameters:

Parameter Type Description
iResponseCode int
strError string
strStatus string

[go to top]


setLogger

Set a logger instance.

public GClient::setLogger(\Psr\Log\LoggerInterface $oLogger) : void

Parameters:

Parameter Type Description
oLogger \Psr\Log\LoggerInterface

[go to top]


setOAuthClient

Set the OAuth2 client configuration from the google API console.

public GClient::setOAuthClient(string $strClientSecrets) : void

The method tries to extract

  • $strClientID
  • $strProjectID
  • $strAuthURI
  • $strTokenURI
  • $strClientSecret
  • $strRedirectURI from the JSON config file.

Parameters:

Parameter Type Description
strClientSecrets string filename

[go to top]


setProjectID

Set the current project ID.

public GClient::setProjectID(string $strProjectID) : void

Parameters:

Parameter Type Description
strProjectID string

[go to top]


setRedirectURI

Set the current redirect URI.

public GClient::setRedirectURI(string $strRedirectURI) : void

Parameters:

Parameter Type Description
strRedirectURI string

[go to top]


setTokenURI

Set the current token URI.

public GClient::setTokenURI(string $strTokenURI) : void

Parameters:

Parameter Type Description
strTokenURI string

[go to top]


Clone this wiki locally