Extension:GoogleAPIClient

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
GoogleAPIClient

Release status:Extension status unmaintained

DescriptionTemplate:Extension#description Provides a copy of the Google PHP API Client to use within MediaWiki
Author(s)Template:Extension#username Florian Schmidt (Florianschmidtwelzowtalk)
MediaWikiTemplate:Extension#mediawiki 1.20+
PHPTemplate:Extension#php 5.3+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license MIT License
Download
Hooks usedTemplate:Extension#hook
UnitTestsListManual:Hooks/UnitTestsList

Translate the GoogleAPIClient extension if it is available at translatewiki.net

Check usage and version matrix.

IssuesPhabricator

Open tasks ยท Report a bug

The Extension GoogleAPIClient provides the google php-api framework to use it within MediaWiki. It provides only backend functions and doesn't add functions or change the interface or something similar.

This extension isn't maintained anymore. You should use the alternative way to install the Google PHP API Library.

Installation[edit]

  • Download and place the file(s) in a directory called GoogleAPIClient in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/GoogleAPIClient/GoogleAPIClient.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Alternative way to install the library without this extension[edit]

If you're already using composer, it makes sense to install the Google API client library for PHP using composer, too (it maybe makes sense to use composer, if you're not using it until now, too). For this, you don't need to install this extension and simply add the following lines to your composer.json (or create one with):

{
  "require": {
    "google/apiclient": "^2.0.0@RC"
  }
}

After running composer update in your extension folder, the client library will be installed. You need to load composer's autoloader to work with the library.

Use API Client[edit]

To use the Google API client in your Extension, simply create a new instance of the class Google_Client. It is already added to $wgAutoloadClasses. To know, how to use the client to get data of Google API services, read the documentation.

Example[edit]

With the following code, we login with a service account with the help of the api client:

// create a new Google_Client object
$client = new Google_Client();
// set app name
$client->setApplicationName( 'your_app_name' );
// Create a new service object (example: Google Analytics)
// The service classes are loaded automatically, too
$service = new Google_Service_Analytics( $client );

// check, if the client is already authenticated
if ( isset( $request->getSessionData( 'service_token' ) ) ) {
	$client->setAccessToken( $request->getSessionData( 'service_token' ) );
}

// load the certificate key file
$key = file_get_contents( 'location_to_certificate' );
// create the service account credentials
$cred = new Google_Auth_AssertionCredentials(
	'service_account_name',
	array( 'https://www.googleapis.com/auth/analytics.readonly' ), // the needed permissions, example: read access to analytics data
	$key
);
// set the credentials
$client->setAssertionCredentials( $cred );
if ( $client->getAuth()->isAccessTokenExpired() ) {
	// authenticate the service account
	$client->getAuth()->refreshTokenWithAssertion( $cred );
}
// set the service_token to the session for future requests
$request->setSessionData( 'service_token', $client->getAccessToken() );

Example extensions[edit]

The following extensions uses GoogleAPIClient (examples):

Google API Client[edit]

This extension contains a copy of the Google PHP API Client, licensed under the Apache License version 2.0. To read the full license text, click here.