How google docs works php

How google docs works php

Do you need to Integrate Google Drive API with PHP? In this educational, we can examine the way to combine the google force API with Hypertext Preprocessor. If you need to combine your PHP script on your google force account doesn`t should be hard. In truth, the usage of the PHP consumer library may be pretty straightforward. In this submission, we’re going to examine the way to create an easy script designed to run on a server. Not this code will NOT run hosted as an internet application. This is only a console script.

Create Installed app credentials

The first component you may want is to create a what is the biggest font on Google docs credetials.json record. This may be created on the google cloud console make certain to create local credentials.

Composer

You will want to put in the Hypertext Preprocessor consumer library this may be carried out with the usage of the composer. Instructions for this may be located in the Google API PHP consumer library README. I propose checking for the maximum latest model variety at the Hypertext Preprocessor applications repository

Authorization

This code is designed to save the person’s authorization inside a record referred to as token.json. After you’ve got legal the app as soon as a refresh token may be saved there. Then whilst the app runs once more it’ll be capable of using the refresh token to request a brand new get admission to token to get admission to your google force account.

Warning refresh tokens will expire after seven days whilst your mission is ready to check out in the google cloud console. Go to the oauth display screen and set it to manufacturing and the refresh token will forestall expiring

The segment of the code which handles the authorization for us is as foll

$authUrl = $consumer->createAuthUrl();

print(“Open the subsequent hyperlink to your browser:n%sn”, $authUrl);

print `Enter verification code: ‘;

$authCode = trim(fgets(STDIN));

// Exchange authorization code to get admission to the token.

$accessToken = $consumer->fetchAccessTokenWithAuthCode($authCode);

$consumer->setAccessToken($accessToken);

Copy the authorization hyperlink and location it in any internet browser. Authorize the application. You will see four hundred blunders withinside the browser window whilst it’s miles whole forget about this. This is because of the elimination of oob. If you examine the URL bar inside that string back is a code. read-only It is this code which you upload on your application. You can best use the code as soon as. The code will then fetch a get admission to a token with this authorization code. Using the subsequent code we save the token back into the token.json record for later use

// Save the token to a record.

if (!file_exists(dirname($tokenPath)))

file_put_contents($tokenPath, json_encode($consumer->getAccessToken()));

Warning refresh tokens will expire after seven days whilst your mission is ready to check out in the google cloud console. Go to the oauth display screen and set it to manufacturing and the refresh token will forestall expiring.

Google Drive API with PHP

Now that your app is allowed you may name the website developer the Google Drive API. Just keep in mind that every technique has one or greater scopes that are required to get admission to that technique. You can locate the scope wished withinside the documentation for every technique. For instance File. the listing will display which of the scopes you need to have asked for authorization from. In this script, I am best inquiring to examine the best scope this means in case you attempt to do a record. create it’s going to now no longer work. If you convert the scope on this script keep in mind to delete the token.json record so that the script will request authorization from the person once more.

use GoogleClient;

use GoogleServiceDrive;

/**

 * Returns an authorized API client.

 * @return Client the authorized client object

 */

function getClient()

{

    $client = new Client();

    $client->setApplicationName(‘Google Drive API PHP Quickstart’);

    $consumer->setScopes(‘https://www.googleapis.com/auth/force.readonly’);

    $consumer->setAuthConfig(‘C:YouTubedevcredentials.json’);

    $consumer->setAccessType(‘offline’);

    $consumer->setRedirectUri(“http://127.0.0.1”);

    $consumer->setPrompt(‘select_account consent’);

    // Load formerly legal token from a record, if it exists.

    // The record token.json shops the people get admission to and refresh tokens, and is

    // created routinely whilst the authorization float completes for the first

    // time.

    $tokenPath = ‘token.json’;

    if (file_exists($tokenPath)) {

        $accessToken = json_decode(file_get_contents($tokenPath), true);

        $consumer->setAccessToken($accessToken);

    }

    // If there may be no preceding token or it is expired.

    if ($consumer->isAccessTokenExpired()) {

        // Refresh the token if possible, else fetch a brand new one.

        if ($consumer->getRefreshToken()) {

            $consumer->fetchAccessTokenWithRefreshToken($consumer->getRefreshToken());

        } else {

            // Request authorization from the person.

            $authUrl = $consumer->createAuthUrl();

            print(“Open the subsequent hyperlink to your browser:n%sn”, $authUrl);

            print ‘Enter verification code: ‘;

            $authCode = trim(fgets(STDIN));

            // Exchange authorization code to get admission to the token.

            $accessToken = $consumer->fetchAccessTokenWithAuthCode($authCode);

            $consumer->setAccessToken($accessToken);

            // Check to look if there have been blunders.

            if (array_key_exists(‘blunders’, $accessToken))

        }

        // Save

Leave a Comment