# Get started with PHP client library

To start using the Google Photos Library API with the PHP client library, you will need to
[set up the client library](https://developers.google.com/photos/library/legacy/guides/get-started-php#get-library) in your development environment.
Before you do that, [configure your project](https://developers.google.com/photos/library/legacy/guides/get-started-php#configure-app) by enabling the API
via the Google API Console and setting up an OAuth 2.0 client ID.

Your application interacts with Google Photos on behalf of a
Google Photos user. For instance, when you create albums in a user's
Google Photos library or upload media items to a user's
Google Photos account, the user authorizes these API requests via the
[OAuth 2.0](https://developers.google.com/identity/protocols/OAuth2) protocol.

The OAuth 2.0 client ID allows your application users to sign in, authenticate,
and thereby use the Library API. The Library API does not support
service accounts; to use this API, users must be signed in to a valid Google
Account.

## Configure your app

### Enable the API

Before you can use the Library API, you must enable it for your project.

1. Go to the [Google API Console](https://console.developers.google.com/apis/library).
2. From the menu bar, select a project or create a new project.
3. To open the Google API Library, from the Navigation menu, select **APIs \& Services \> Library**.
4. Search for "Google Photos Library API". Select the correct result and click **Enable**.

### Request an OAuth 2.0 client ID

Follow the steps below to request an OAuth client ID and configure it for your
application. This example uses an application where the entire OAuth flow is
handled server-side, such as the one in our samples. The setup process may vary
for other [implementation scenarios](https://developers.google.com/identity/protocols/OAuth2).

1. Go to the [Google API Console](https://console.developers.google.com/apis/library) and select your project.
2. From the menu, select **APIs \& Services \> Credentials**.
3. On the **Credentials** page, click **Create Credentials \> OAuth client ID**.
4. Select your **Application type** . In this example, the application type is **Web application**.
5. Register the origins from which your app is allowed to access the Google APIs
   as follows:

   1. To identify the client ID, enter a name.
   2. In the **Authorized JavaScript origins** field, enter the origin for your
      app. This field doesn't allow wildcards.

      You can enter multiple origins to allow your app to run on different
      protocols, domains, or subdomains. The URLs you enter are allowed to
      start an OAuth request.

      The following example shows a local development URL (our samples use
      `localhost:8080`) and a production URL.

          http://localhost:8080
          https://myproductionurl.example.com

   3. The **Authorized redirect URI** field is the endpoint that receives
      responses from the OAuth 2.0 server. Typically, this includes your
      development environment and points to a path in your application.

          http://localhost:8080/auth/google/callback
          https://myproductionurl.example.com/auth/google/callback

   4. Click **Create**.

<!-- -->

6. From the resulting OAuth client dialog, download the JSON file
   containing your client configuration. You client details consist of the
   following:

   - Client ID
   - Client secret

   This JSON file will be used later to [set up](https://developers.google.com/photos/library/legacy/guides/get-started-php#get-library)
   the Google Auth library for PHP which works with this client library.

Before you can launch a public application that accesses the Library API,
your app must be reviewed by Google. An "Unverified app" message appears on the
screen when you test your application, until it is
[verified](https://support.google.com/cloud/answer/7454865).

## Set up the client library

The PHP client library handles all the backend API calls for you, and exposes
friendly objects to work with, including code samples for some common API tasks.
Firstly, download and install the Google Photos Library API client library for PHP along with
the dependencies from [GitHub](https://github.com/google/php-photoslibrary).
Then, set up your OAuth2 credentials for PHP.

### Download options

Use [composer](https://github.com/google/php-photoslibrary#using-composer-require)
to include the library as a dependency in your development environment. Run the
following command to add the library to your project configuration and download
it to the `vendor/` directory.

    composer require google/photos-library

Alternatively, you can also
[clone the
repository](https://github.com/google/php-photoslibrary#using-git-clone) or
[download a compressed
tarball](https://github.com/google/php-photoslibrary#downloading-a-compressed-tarball).

### Set up your OAuth2 credentials for PHP

This client library works with the
[Google Auth Library for
PHP](https://github.com/google/google-auth-library-php). For more information,
refer to [Using OAuth 2.0 with the Google API Client Library for
PHP](https://developers.google.com/api-client-library/php/auth/web-app).

Use the authentication credentials returned by the auth library when setting up
the `PhotosLibraryClient`.

## Try out some samples

Try the code below to make your first API call using the PHP client library.

    use Google\Auth\Credentials\UserRefreshCredentials;
    use Google\Photos\Library\V1\PhotosLibraryClient;
    use Google\Photos\Library\V1\PhotosLibraryResourceFactory;

    try {
        // Use the OAuth flow provided by the Google API Client Auth library
        // to authenticate users. See the file /src/common/common.php in the samples for a complete
        // authentication example.
        $authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ );

        // Set up the Photos Library Client that interacts with the API
        $photosLibraryClient = new PhotosLibraryClient(['credentials' => $authCredentials]);

        // Create a new Album object with at title
        $newAlbum = PhotosLibraryResourceFactory::album("My Album");

        // Make the call to the Library API to create the new album
        $createdAlbum = $photosLibraryClient->createAlbum($newAlbum);

        // The creation call returns the ID of the new album
        $albumId = $createdAlbum->getId();
    } catch (\Google\ApiCore\ApiException $exception) {
        // Error during album creation
    } catch (\Google\ApiCore\ValidationException $e) {
        // Error during client creation
        echo $exception;
    }

There are more
[samples](https://github.com/google/php-photoslibrary/tree/samples) on GitHub
for you to try.