-
Notifications
You must be signed in to change notification settings - Fork 1
Class GClient
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:
| Constant | Description |
|---|---|
GET |
GET request |
POST |
POST request |
PUT |
PUT request |
PATCH |
PATCH request |
DELETE |
DELETE request |
| 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. |
public GClient::__construct(\Psr\Log\LoggerInterface $oLogger = null)Parameters:
| Parameter | Type | Description |
|---|---|---|
oLogger |
\Psr\Log\LoggerInterface |
Add scope that is neede for following API requests.
public GClient::addScope(string|string[] $scope) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
scope |
string | string[] |
Build the URL to call the google OAuth2.
public GClient::buildAuthURL(string $strLoginHint = '') : stringDescription 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
Cleint request to the API.
public GClient::fetchJsonResponse(string $strURI, int $iMethod, string[] $aHeader = [], string $data = '') : string|falseParameters:
| Parameter | Type | Description |
|---|---|---|
strURI |
string | |
iMethod |
int | |
aHeader |
string[] | |
data |
string |
Return Type: string|false
Send request to get access and refresh token from a passed auth code.
public GClient::fetchTokens(string $strAuthCode) : boolParameters:
| Parameter | Type | Description |
|---|---|---|
strAuthCode |
string | the code passed from accounts.google.com |
Return Type: bool
The current set access token.
public GClient::getAccessToken() : arrayReturn Type: array
Get the OAuth HTTP header for API requests.
public GClient::getAuthHeader() : stringReturn Type: string
Getter for the current auth URI.
public GClient::getAuthURI() : stringReturn Type: string
Getter for the current client ID.
public GClient::getClientID() : stringReturn Type: string
Getter for the current client secret.
public GClient::getClientSecret() : stringReturn Type: string
Error text if the last API request has failed.
public GClient::getLastError() : stringReturn Type: string
Response code of the last API request.
public GClient::getLastResponseCode() : intReturn Type: int
Status if the last API request has failed.
public GClient::getLastStatus() : stringReturn Type: string
Getter for the current project ID.
public GClient::getProjectID() : stringReturn Type: string
Getter for the current redirect URI.
public GClient::getRedirectURI() : stringReturn Type: string
Getter for the current refresh token.
public GClient::getRefreshToken() : stringReturn Type: string
Getter for the current token URI.
public GClient::getTokenURI() : stringReturn Type: string
Check, if the actual set access token has expired.
public GClient::isAccessTokenExpired(int $iOffset = 20) : boolIt 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
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>
Refresh expired access token.
public GClient::refreshAccessToken(string $strRefreshToken) : arrayParameters:
| Parameter | Type | Description |
|---|---|---|
strRefreshToken |
string |
Return Type: array
new access token
Set a saved access token.
public GClient::setAccessToken(string|array $token) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
token |
string | array | accesstoken as string (JSON) or array |
Set the current auth URI.
public GClient::setAuthURI(string $strAuthURI) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strAuthURI |
string |
Set the current client ID.
public GClient::setClientID(string $strClientID) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strClientID |
string |
Set the current client secret.
public GClient::setClientSecret(string $strClientSecret) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strClientSecret |
string |
Set information about the last error occured.
public GClient::setError(int $iResponseCode, string $strError, string $strStatus) : voidIf 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 |
Set a logger instance.
public GClient::setLogger(\Psr\Log\LoggerInterface $oLogger) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
oLogger |
\Psr\Log\LoggerInterface |
Set the OAuth2 client configuration from the google API console.
public GClient::setOAuthClient(string $strClientSecrets) : voidThe method tries to extract
- $strClientID
- $strProjectID
- $strAuthURI
- $strTokenURI
- $strClientSecret
- $strRedirectURI from the JSON config file.
Parameters:
| Parameter | Type | Description |
|---|---|---|
strClientSecrets |
string | filename |
Set the current project ID.
public GClient::setProjectID(string $strProjectID) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strProjectID |
string |
Set the current redirect URI.
public GClient::setRedirectURI(string $strRedirectURI) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strRedirectURI |
string |
Set the current token URI.
public GClient::setTokenURI(string $strTokenURI) : voidParameters:
| Parameter | Type | Description |
|---|---|---|
strTokenURI |
string |
build with phpDocumentor 3 using template phpdoc2githubwiki / (2023-04-26)