diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 000000000..63ede231b
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,13 @@
+/.gitattributes export-ignore
+/.github export-ignore
+/.gitignore export-ignore
+/.travis.yml export-ignore
+/CODE_OF_CONDUCT.md export-ignore
+/docs export-ignore
+/examples export-ignore
+/phpunit.xml.dist export-ignore
+/phpcs.xml.dist export-ignore
+/style export-ignore
+/tests export-ignore
+/UPGRADING.md export-ignore
+/phpstan.neon.dist export-ignore
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
new file mode 100644
index 000000000..c20bc3167
--- /dev/null
+++ b/.github/CODEOWNERS
@@ -0,0 +1,7 @@
+# Code owners file.
+# This file controls who is tagged for review for any given pull request.
+#
+# For syntax help see:
+# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax
+
+* @googleapis/yoshi-php
diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md
similarity index 95%
rename from CONTRIBUTING.md
rename to .github/CONTRIBUTING.md
index e852a93ac..d8deaf3a0 100644
--- a/CONTRIBUTING.md
+++ b/.github/CONTRIBUTING.md
@@ -7,7 +7,7 @@ We'd love to accept your code patches! However, before we can take them, we have
Please fill out either the individual or corporate Contributor License Agreement (CLA).
* If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html).
- * If you work for a company that wants to allow you to contribute your work to this client library, then you'll need to sign a[corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html).
+ * If you work for a company that wants to allow you to contribute your work to this client library, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html).
Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll add you to the official list of contributors and be able to accept your patches.
diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md
new file mode 100644
index 000000000..e70487ba1
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug_report.md
@@ -0,0 +1,36 @@
+---
+name: Bug report
+about: Create a report to help us improve
+
+---
+
+Thanks for stopping by to let us know something could be better!
+
+**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response.
+
+Please run down the following list and make sure you've tried the usual "quick fixes":
+
+ - Search the issues already opened: https://github.com/googleapis/google-api-php-client/issues
+ - Search StackOverflow: http://stackoverflow.com/questions/tagged/google-cloud-platform+php
+
+If you are still having issues, please be sure to include as much information as possible:
+
+#### Environment details
+
+ - OS:
+ - PHP version:
+ - Package name and version:
+
+#### Steps to reproduce
+
+ 1. ...
+
+#### Code example
+
+```php
+# example
+```
+
+Making sure to follow these steps will guarantee the quickest resolution possible.
+
+Thanks!
diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md
new file mode 100644
index 000000000..20d075c25
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/feature_request.md
@@ -0,0 +1,21 @@
+---
+name: Feature request
+about: Suggest an idea for this library
+
+---
+
+Thanks for stopping by to let us know something could be better!
+
+**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response.
+
+ **Is your feature request related to a problem? Please describe.**
+A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
+
+ **Describe the solution you'd like**
+A clear and concise description of what you want to happen.
+
+ **Describe alternatives you've considered**
+A clear and concise description of any alternative solutions or features you've considered.
+
+ **Additional context**
+Add any other context or screenshots about the feature request here.
diff --git a/.github/ISSUE_TEMPLATE/support_request.md b/.github/ISSUE_TEMPLATE/support_request.md
new file mode 100644
index 000000000..995869032
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/support_request.md
@@ -0,0 +1,7 @@
+---
+name: Support request
+about: If you have a support contract with Google, please create an issue in the Google Cloud Support console.
+
+---
+
+**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response.
diff --git a/.github/release-please.yml b/.github/release-please.yml
new file mode 100644
index 000000000..0a6e0cc27
--- /dev/null
+++ b/.github/release-please.yml
@@ -0,0 +1,3 @@
+releaseType: simple
+handleGHRelease: true
+primaryBranch: main
diff --git a/.github/release-trigger.yml b/.github/release-trigger.yml
new file mode 100644
index 000000000..ed17d0705
--- /dev/null
+++ b/.github/release-trigger.yml
@@ -0,0 +1,2 @@
+enabled: true
+multiScmName: google-api-php-client
diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml
new file mode 100644
index 000000000..a4444366a
--- /dev/null
+++ b/.github/sync-repo-settings.yaml
@@ -0,0 +1,24 @@
+rebaseMergeAllowed: true
+squashMergeAllowed: true
+mergeCommitAllowed: false
+branchProtectionRules:
+- pattern: master
+ isAdminEnforced: true
+ requiredStatusCheckContexts:
+ - 'PHP 8.0 Unit Test'
+ - 'PHP 8.0 --prefer-lowest Unit Test'
+ - 'PHP 8.1 Unit Test'
+ - 'PHP 8.2 Unit Test'
+ - 'PHP 8.3 Unit Test'
+ - 'PHP 8.4'
+ - 'PHP 8.4 --prefer-lowest Unit Test'
+ - 'PHP Style Check'
+ - 'cla/google'
+ requiredApprovingReviewCount: 1
+ requiresCodeOwnerReviews: true
+ requiresStrictStatusChecks: true
+permissionRules:
+ - team: yoshi-php-admins
+ permission: admin
+ - team: yoshi-php
+ permission: push
diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml
new file mode 100644
index 000000000..1f553fbb9
--- /dev/null
+++ b/.github/workflows/asset-release.yml
@@ -0,0 +1,64 @@
+name: Add Release Assets
+
+on:
+ release:
+ types: [published]
+ workflow_dispatch:
+
+jobs:
+ asset:
+ runs-on: ${{ matrix.operating-system }}
+ strategy:
+ matrix:
+ operating-system: [ ubuntu-latest ]
+ php: [ "8.0", "8.3" ]
+
+ name: Upload Release Assets
+ steps:
+ - id: getTag
+ name: Get Tag
+ run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
+
+ - name: Get release
+ id: get_release
+ uses: bruceadams/get-release@v1.2.2
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+ - name: Checkout
+ uses: actions/checkout@v2
+
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: ${{ matrix.php }}
+
+ - name: Install Dependencies
+ uses: nick-invision/retry@v1
+ with:
+ timeout_minutes: 10
+ max_attempts: 3
+ command: composer remove --no-update --dev cache/filesystem-adapter && composer install --no-dev --prefer-dist
+
+ - name: Create Archive
+ run: |
+ zip -r ${fileName} . &&
+ zip -d ${fileName} ".git*" || true &&
+ zip -d ${fileName} "tests*" || true &&
+ zip -d ${fileName} "docs*" || true &&
+ zip -d ${fileName} "phpcs.xml.dist" || true &&
+ zip -d ${fileName} "phpunit.xml.dist" || true &&
+ zip -d ${fileName} "tests*" || true &&
+ zip -d ${fileName} "examples*" || true
+ env:
+ fileName: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip
+
+ - name: Upload Release Archive
+ uses: actions/upload-release-asset@v1
+ env:
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
+ with:
+ upload_url: ${{ steps.get_release.outputs.upload_url }}
+ asset_path: ./google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip
+ asset_name: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip
+ asset_content_type: application/zip
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
new file mode 100644
index 000000000..5d09dabb0
--- /dev/null
+++ b/.github/workflows/docs.yml
@@ -0,0 +1,26 @@
+name: Generate Documentation
+on:
+ push:
+ tags:
+ - "*"
+ workflow_dispatch:
+ inputs:
+ tag:
+ description: 'Tag to generate documentation for'
+ required: false
+ pull_request:
+
+permissions:
+ contents: write
+
+jobs:
+ docs:
+ name: "Generate and Deploy Documentation"
+ uses: GoogleCloudPlatform/php-tools/.github/workflows/doctum.yml@main
+ with:
+ title: "Google Cloud PHP Client"
+ default_version: ${{ inputs.tag || github.head_ref || github.ref_name }}
+ exclude_file: aliases.php
+ tag_pattern: "v2.*"
+ dry_run: ${{ github.event_name == 'pull_request' }}
+
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
new file mode 100644
index 000000000..9f86e493a
--- /dev/null
+++ b/.github/workflows/tests.yml
@@ -0,0 +1,68 @@
+name: Test Suite
+on:
+ push:
+ branches:
+ - main
+ pull_request:
+
+jobs:
+ test:
+ runs-on: ubuntu-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ]
+ composer-flags: [""]
+ include:
+ - php: "8.0"
+ composer-flags: "--prefer-lowest "
+ - php: "8.4"
+ composer-flags: "--prefer-lowest "
+ name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test
+ steps:
+ - uses: actions/checkout@v2
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: ${{ matrix.php }}
+ - name: Install Dependencies
+ uses: nick-invision/retry@v2
+ with:
+ timeout_minutes: 10
+ max_attempts: 3
+ command: composer update ${{ matrix.composer-flags }}
+ - name: Run Script
+ run: vendor/bin/phpunit
+
+ style:
+ runs-on: ubuntu-latest
+ name: PHP Style Check
+ steps:
+ - uses: actions/checkout@v2
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: "8.1"
+ - name: Install Dependencies
+ uses: nick-invision/retry@v2
+ with:
+ timeout_minutes: 10
+ max_attempts: 3
+ command: composer install
+ - name: Run Script
+ run: vendor/bin/phpcs src tests examples --standard=phpcs.xml.dist -nps
+ staticanalysis:
+ runs-on: ubuntu-latest
+ name: PHPStan Static Analysis
+ steps:
+ - uses: actions/checkout@v2
+ - name: Install PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: '8.1'
+ - name: Run Script
+ run: |
+ composer install
+ composer global require phpstan/phpstan
+ ~/.composer/vendor/bin/phpstan analyse
+
diff --git a/.gitignore b/.gitignore
index 0a70c65d2..fc8518be0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,10 @@
.DS_Store
phpunit.xml
+phpcs.xml
composer.lock
vendor
examples/testfile-small.txt
examples/testfile.txt
tests/.apiKey
+.phpunit.result.cache
+.idea/
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 11b8eeddd..000000000
--- a/.travis.yml
+++ /dev/null
@@ -1,47 +0,0 @@
-language: php
-
-services:
- - memcached
-
-env:
- global:
- - MEMCACHE_HOST=127.0.0.1
- - MEMCACHE_PORT=11211
- matrix:
- - GUZZLE_VERSION=~5.2
- - GUZZLE_VERSION=~6.0
-
-sudo: false
-
-cache:
- directories:
- - $HOME/.composer/cache
-
-php:
- - 5.4
- - 5.5
- - 5.6
- - 7.0
- - hhvm
-
-# Guzzle 6.0 is not compatible with PHP 5.4
-matrix:
- exclude:
- - php: 5.4
- env: GUZZLE_VERSION=~6.0
-
-before_install:
- - composer self-update
-
-install:
- - composer install
- - composer require guzzlehttp/guzzle:$GUZZLE_VERSION
-
-before_script:
- - phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
- - phpenv version-name | grep ^5.[34] && echo "apc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
-
-script:
- - vendor/bin/phpunit
- - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi
-
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 000000000..9fd015477
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,123 @@
+# Changelog
+
+## [2.18.3](https://github.com/googleapis/google-api-php-client/compare/v2.18.2...v2.18.3) (2025-04-08)
+
+
+### Bug Fixes
+
+* Convert Finder lazy iterator to array before deletion ([#2663](https://github.com/googleapis/google-api-php-client/issues/2663)) ([c699405](https://github.com/googleapis/google-api-php-client/commit/c6994051af1568359c97d267d9ef34ccbda31387))
+
+## [2.18.2](https://github.com/googleapis/google-api-php-client/compare/v2.18.1...v2.18.2) (2024-12-16)
+
+
+### Bug Fixes
+
+* Correct type for jwt constructor arg ([#2648](https://github.com/googleapis/google-api-php-client/issues/2648)) ([31a9861](https://github.com/googleapis/google-api-php-client/commit/31a9861af02a8e9070b395f05caed7ffce0ef8be))
+
+## [2.18.1](https://github.com/googleapis/google-api-php-client/compare/v2.18.0...v2.18.1) (2024-11-24)
+
+
+### Bug Fixes
+
+* Implicitly marking parameter as nullable is deprecated ([#2638](https://github.com/googleapis/google-api-php-client/issues/2638)) ([de57db2](https://github.com/googleapis/google-api-php-client/commit/de57db2fdc0d56de1abbf778b28b77c3347eb3fd))
+
+## [2.18.0](https://github.com/googleapis/google-api-php-client/compare/v2.17.0...v2.18.0) (2024-10-16)
+
+
+### Features
+
+* **docs:** Use doctum shared workflow for reference docs ([#2618](https://github.com/googleapis/google-api-php-client/issues/2618)) ([242e2cb](https://github.com/googleapis/google-api-php-client/commit/242e2cb09ad5b25b047a862b4d521037e74cae29))
+
+
+### Bug Fixes
+
+* Explicit token caching issue ([#2358](https://github.com/googleapis/google-api-php-client/issues/2358)) ([dc13e5e](https://github.com/googleapis/google-api-php-client/commit/dc13e5e3f517148d3c66d151a5ab133b7840d8fb))
+
+## [2.17.0](https://github.com/googleapis/google-api-php-client/compare/v2.16.0...v2.17.0) (2024-07-10)
+
+
+### Features
+
+* Add logger to client constructor config ([#2606](https://github.com/googleapis/google-api-php-client/issues/2606)) ([1f47133](https://github.com/googleapis/google-api-php-client/commit/1f4713329d71111a317cda8ef8603fa1bdc88858))
+* Add the protected apiVersion property ([#2588](https://github.com/googleapis/google-api-php-client/issues/2588)) ([7e79f3d](https://github.com/googleapis/google-api-php-client/commit/7e79f3d7be4811f760e19cc4a2c558e04196ec1d))
+
+## [2.16.0](https://github.com/googleapis/google-api-php-client/compare/v2.15.4...v2.16.0) (2024-04-24)
+
+
+### Features
+
+* Add universe domain support ([#2563](https://github.com/googleapis/google-api-php-client/issues/2563)) ([35895de](https://github.com/googleapis/google-api-php-client/commit/35895ded90b507074b3430a94a5790ddd01f39f0))
+
+## [2.15.4](https://github.com/googleapis/google-api-php-client/compare/v2.15.3...v2.15.4) (2024-03-06)
+
+
+### Bug Fixes
+
+* Updates phpseclib because of a security issue ([#2574](https://github.com/googleapis/google-api-php-client/issues/2574)) ([633d41f](https://github.com/googleapis/google-api-php-client/commit/633d41f1b65fdb71a83bf747f7a3ad9857f6d02a))
+
+## [2.15.3](https://github.com/googleapis/google-api-php-client/compare/v2.15.2...v2.15.3) (2024-01-04)
+
+
+### Bug Fixes
+
+* Guzzle dependency version ([#2546](https://github.com/googleapis/google-api-php-client/issues/2546)) ([c270f28](https://github.com/googleapis/google-api-php-client/commit/c270f28b00594a151a887edd3cfd205594a1256a))
+
+## [2.15.2](https://github.com/googleapis/google-api-php-client/compare/v2.15.1...v2.15.2) (2024-01-03)
+
+
+### Bug Fixes
+
+* Disallow vulnerable guzzle versions ([#2536](https://github.com/googleapis/google-api-php-client/issues/2536)) ([d1830ed](https://github.com/googleapis/google-api-php-client/commit/d1830ede17114a4951ab9e60b3b9bcd9393b8668))
+* Php 8.3 deprecated get_class method call without argument ([#2509](https://github.com/googleapis/google-api-php-client/issues/2509)) ([8c66021](https://github.com/googleapis/google-api-php-client/commit/8c6602119b631e1a9da4dbe219af18d51c8dab8e))
+* Phpseclib security vulnerability ([#2524](https://github.com/googleapis/google-api-php-client/issues/2524)) ([73705c2](https://github.com/googleapis/google-api-php-client/commit/73705c2a65bfc01fa6d7717b7f401b8288fe0587))
+
+## [2.15.1](https://github.com/googleapis/google-api-php-client/compare/v2.15.0...v2.15.1) (2023-09-12)
+
+
+### Bug Fixes
+
+* Upgrade min phpseclib version ([#2499](https://github.com/googleapis/google-api-php-client/issues/2499)) ([8e7fae2](https://github.com/googleapis/google-api-php-client/commit/8e7fae2b79cfc1b72026347abf6314d91442a018))
+
+## [2.15.0](https://github.com/googleapis/google-api-php-client/compare/v2.14.0...v2.15.0) (2023-05-18)
+
+
+### Features
+
+* Add pkce support and upgrade examples ([#2438](https://github.com/googleapis/google-api-php-client/issues/2438)) ([bded223](https://github.com/googleapis/google-api-php-client/commit/bded223ece445a6130cde82417b20180b1d6698a))
+* Drop support for 7.3 and below ([#2431](https://github.com/googleapis/google-api-php-client/issues/2431)) ([c765b37](https://github.com/googleapis/google-api-php-client/commit/c765b379e95ab272b6a87aa802d9f5507eaeb2e7))
+
+## [2.14.0](https://github.com/googleapis/google-api-php-client/compare/v2.13.2...v2.14.0) (2023-05-11)
+
+
+### Features
+
+* User-supplied query params for auth url ([#2432](https://github.com/googleapis/google-api-php-client/issues/2432)) ([74a7d7b](https://github.com/googleapis/google-api-php-client/commit/74a7d7b838acb08afc02b449f338fbe6577cb03c))
+
+## [2.13.2](https://github.com/googleapis/google-api-php-client/compare/v2.13.1...v2.13.2) (2023-03-23)
+
+
+### Bug Fixes
+
+* Calling class_exists with null in Google\Model ([#2405](https://github.com/googleapis/google-api-php-client/issues/2405)) ([5ed4edc](https://github.com/googleapis/google-api-php-client/commit/5ed4edc9315110a715e9763d27ee6761e1aaa00a))
+
+## [2.13.1](https://github.com/googleapis/google-api-php-client/compare/v2.13.0...v2.13.1) (2023-03-13)
+
+
+### Bug Fixes
+
+* Allow dynamic properties on model classes ([#2408](https://github.com/googleapis/google-api-php-client/issues/2408)) ([11080d5](https://github.com/googleapis/google-api-php-client/commit/11080d5e85a040751a13aca8131f93c7d910db11))
+
+## [2.13.0](https://github.com/googleapis/google-api-php-client/compare/v2.12.6...v2.13.0) (2022-12-19)
+
+
+### Features
+
+* Make auth http client config extends from default client config ([#2348](https://github.com/googleapis/google-api-php-client/issues/2348)) ([2640250](https://github.com/googleapis/google-api-php-client/commit/2640250c7bab479f378972733dcc0a3e9b2e14f8))
+
+
+### Bug Fixes
+
+* Don't send content-type header if no post body exists ([#2288](https://github.com/googleapis/google-api-php-client/issues/2288)) ([654c0e2](https://github.com/googleapis/google-api-php-client/commit/654c0e29ab78aba8bfef52fd3d06a3b2b39c4e0d))
+* Ensure new redirect_uri propogates to OAuth2 class ([#2282](https://github.com/googleapis/google-api-php-client/issues/2282)) ([a69131b](https://github.com/googleapis/google-api-php-client/commit/a69131b6488735d112a529a278cfc8b875e18647))
+* Lint errors ([#2315](https://github.com/googleapis/google-api-php-client/issues/2315)) ([88cc63c](https://github.com/googleapis/google-api-php-client/commit/88cc63c38b0cf88629f66fdf8ba6006f6c6d5a2c))
+* Update accounts.google.com authorization URI ([#2275](https://github.com/googleapis/google-api-php-client/issues/2275)) ([b2624d2](https://github.com/googleapis/google-api-php-client/commit/b2624d21fce894126b9975a872cf5cda8038b254))
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
new file mode 100644
index 000000000..46b2a08ea
--- /dev/null
+++ b/CODE_OF_CONDUCT.md
@@ -0,0 +1,43 @@
+# Contributor Code of Conduct
+
+As contributors and maintainers of this project,
+and in the interest of fostering an open and welcoming community,
+we pledge to respect all people who contribute through reporting issues,
+posting feature requests, updating documentation,
+submitting pull requests or patches, and other activities.
+
+We are committed to making participation in this project
+a harassment-free experience for everyone,
+regardless of level of experience, gender, gender identity and expression,
+sexual orientation, disability, personal appearance,
+body size, race, ethnicity, age, religion, or nationality.
+
+Examples of unacceptable behavior by participants include:
+
+* The use of sexualized language or imagery
+* Personal attacks
+* Trolling or insulting/derogatory comments
+* Public or private harassment
+* Publishing other's private information,
+such as physical or electronic
+addresses, without explicit permission
+* Other unethical or unprofessional conduct.
+
+Project maintainers have the right and responsibility to remove, edit, or reject
+comments, commits, code, wiki edits, issues, and other contributions
+that are not aligned to this Code of Conduct.
+By adopting this Code of Conduct,
+project maintainers commit themselves to fairly and consistently
+applying these principles to every aspect of managing this project.
+Project maintainers who do not follow or enforce the Code of Conduct
+may be permanently removed from the project team.
+
+This code of conduct applies both within project spaces and in public spaces
+when an individual is representing the project or its community.
+
+Instances of abusive, harassing, or otherwise unacceptable behavior
+may be reported by opening an issue
+or contacting one or more of the project maintainers.
+
+This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
+available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
diff --git a/README.md b/README.md
index e307595e6..7bac1824f 100644
--- a/README.md
+++ b/README.md
@@ -1,18 +1,35 @@
-[](https://travis-ci.org/google/google-api-php-client)
+
# Google APIs Client Library for PHP #
-## Description ##
-The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server.
+**NOTE**: please check to see if the package you'd like to install is available in our
+list of [Google cloud packages](https://cloud.google.com/php/docs/reference) first, as
+these are the recommended libraries.
-## Beta ##
-This library is in Beta. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will make an effort to support the public and protected surface of the library and maintain backwards compatibility in the future. While we are still in Beta, we reserve the right to make incompatible changes.
+
+
+The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your server.
+
+These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
+
+## Google Cloud Platform
+
+For Google Cloud Platform APIs such as [Datastore][cloud-datastore], [Cloud Storage][cloud-storage], [Pub/Sub][cloud-pubsub], and [Compute Engine][cloud-compute], we recommend using the Google Cloud client libraries. For a complete list of supported Google Cloud client libraries, see [googleapis/google-cloud-php](https://github.com/googleapis/google-cloud-php).
+
+[cloud-datastore]: https://github.com/googleapis/google-cloud-php-datastore
+[cloud-pubsub]: https://github.com/googleapis/google-cloud-php-pubsub
+[cloud-storage]: https://github.com/googleapis/google-cloud-php-storage
+[cloud-compute]: https://github.com/googleapis/google-cloud-php-compute
## Requirements ##
-* [PHP 5.4.0 or higher](http://www.php.net/)
+* [PHP 8.0 or higher](https://www.php.net/)
## Developer Documentation ##
-http://developers.google.com/api-client-library/php
+
+The [docs folder](docs/) provides detailed guides for using this library.
## Installation ##
@@ -20,14 +37,23 @@ You can use **Composer** or simply **Download the Release**
### Composer
-The preferred method is via [composer](https://getcomposer.org). Follow the
+The preferred method is via [composer](https://getcomposer.org/). Follow the
[installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have
composer installed.
Once composer is installed, execute the following command in your project root to install this library:
```sh
-composer require google/apiclient:^2.0
+composer require google/apiclient
+```
+
+If you're facing a timeout error then either increase the timeout for composer by adding the env flag as `COMPOSER_PROCESS_TIMEOUT=600 composer install` or you can put this in the `config` section of the composer schema:
+```
+{
+ "config": {
+ "process-timeout": 600
+ }
+}
```
Finally, be sure to include the autoloader:
@@ -36,9 +62,64 @@ Finally, be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';
```
+This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the [latest version](https://github.com/googleapis/google-api-php-client-services/releases) yourself prior to using this library in production.
+
+#### Cleaning up unused services
+
+There are over 200 Google API services. The chances are good that you will not
+want them all. In order to avoid shipping these dependencies with your code,
+you can run the `Google\Task\Composer::cleanup` task and specify the services
+you want to keep in `composer.json`:
+
+```json
+{
+ "require": {
+ "google/apiclient": "^2.15.0"
+ },
+ "scripts": {
+ "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
+ },
+ "extra": {
+ "google/apiclient-services": [
+ "Drive",
+ "YouTube"
+ ]
+ }
+}
+```
+
+This example will remove all services other than "Drive" and "YouTube" when
+`composer update` or a fresh `composer install` is run.
+
+**IMPORTANT**: If you add any services back in `composer.json`, you will need to
+remove the `vendor/google/apiclient-services` directory explicitly for the
+change you made to have effect:
+
+```sh
+rm -r vendor/google/apiclient-services
+composer update
+```
+
+**NOTE**: This command performs an exact match on the service name, so to keep
+`YouTubeReporting` and `YouTubeAnalytics` as well, you'd need to add each of
+them explicitly:
+
+```json
+{
+ "extra": {
+ "google/apiclient-services": [
+ "Drive",
+ "YouTube",
+ "YouTubeAnalytics",
+ "YouTubeReporting"
+ ]
+ }
+}
+```
+
### Download the Release
-If you abhor using composer, you can download the package in its entirety. The [Releases](https://github.com/google/google-api-php-client/releases) page lists all stable versions. Download any file
+If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file
with the name `google-api-php-client-[RELEASE_NAME].zip` for a package including this library and its dependencies.
Uncompress the zip file you download, and include the autoloader in your project:
@@ -47,7 +128,7 @@ Uncompress the zip file you download, and include the autoloader in your project
require_once '/path/to/google-api-php-client/vendor/autoload.php';
```
-For additional installation and setup instructions, see [the documentation](https://developers.google.com/api-client-library/php/start/installation).
+For additional installation and setup instructions, see [the documentation](docs/).
## Examples ##
See the [`examples/`](examples) directory for examples of the key client features. You can
@@ -66,15 +147,18 @@ And then browsing to the host and port you specified
// include your composer dependencies
require_once 'vendor/autoload.php';
-$client = new Google_Client();
+$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");
-$service = new Google_Service_Books($client);
-$optParams = array('filter' => 'free-ebooks');
-$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+$service = new Google\Service\Books($client);
+$query = 'Henry David Thoreau';
+$optParams = [
+ 'filter' => 'free-ebooks',
+];
+$results = $service->volumes->listVolumes($query, $optParams);
-foreach ($results as $item) {
+foreach ($results->getItems() as $item) {
echo $item['volumeInfo']['title'], " \n";
}
```
@@ -83,34 +167,48 @@ foreach ($results as $item) {
> An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php).
-**NOTE:** If you are using Google App Engine or Google Compute Engine, you can skip steps 1-3, as Application Default Credentials are included automatically when `useApplicationDefaultCredentials` is called.
-
-1. Follow the instructions to [Create Web Application Credentials](https://developers.google.com/api-client-library/php/auth/web-app#creatingcred)
+1. Follow the instructions to [Create Web Application Credentials](docs/oauth-web.md#create-authorization-credentials)
1. Download the JSON credentials
-1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
+1. Set the path to these credentials using `Google\Client::setAuthConfig`:
```php
- putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
+ $client = new Google\Client();
+ $client->setAuthConfig('/path/to/client_credentials.json');
```
-1. Tell the Google client to use your service account credentials to authenticate:
+1. Set the scopes required for the API you are going to call
```php
- $client = new Google_Client();
- $client->useApplicationDefaultCredentials();
+ $client->addScope(Google\Service\Drive::DRIVE);
```
-1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject:
+1. Set your application's redirect URI
+
+ ```php
+ // Your redirect URI can be any registered URI, but in this example
+ // we redirect back to this same page
+ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
+ $client->setRedirectUri($redirect_uri);
+ ```
+
+1. In the script handling the redirect URI, exchange the authorization code for an access token:
```php
- $ client->setSubject($user_to_impersonate);
+ if (isset($_GET['code'])) {
+ $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
+ }
```
### Authentication with Service Accounts ###
> An example of this can be seen in [`examples/service-account.php`](examples/service-account.php).
-1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount)
+Some APIs
+(such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do
+not support service accounts. Check with the specific API documentation if API
+calls return unexpected 401 or 403 errors.
+
+1. Follow the instructions to [Create a Service Account](docs/oauth-server.md#creating-a-service-account)
1. Download the JSON credentials
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
@@ -121,14 +219,14 @@ foreach ($results as $item) {
1. Tell the Google client to use your service account credentials to authenticate:
```php
- $client = new Google_Client();
+ $client = new Google\Client();
$client->useApplicationDefaultCredentials();
```
1. Set the scopes required for the API you are going to call
```php
- $client->addScope(Google_Service_Drive::DRIVE);
+ $client->addScope(Google\Service\Drive::DRIVE);
```
1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject:
@@ -137,15 +235,29 @@ foreach ($results as $item) {
$client->setSubject($user_to_impersonate);
```
+#### How to use a specific JSON key
+
+If you want to a specific JSON key instead of using `GOOGLE_APPLICATION_CREDENTIALS` environment variable, you can do this:
+
+```php
+$jsonKey = [
+ 'type' => 'service_account',
+ // ...
+];
+$client = new Google\Client();
+$client->setAuthConfig($jsonKey);
+```
+
### Making Requests ###
-The classes used to call the API in [google-api-php-client-services](https://github.com/Google/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/).
+The classes used to call the API in [google-api-php-client-services](https://github.com/googleapis/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/).
A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this:
-```json
+```
POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY
-
+```
+```json
{
"query": {
"kind": [{
@@ -166,10 +278,10 @@ Using this library, the same call would look something like this:
```php
// create the datastore service class
-$datastore = new Google_Service_Datastore($client)
+$datastore = new Google\Service\Datastore($client);
// build the query - this maps directly to the JSON
-$query = new Google_Service_Datastore_Query([
+$query = new Google\Service\Datastore\Query([
'kind' => [
[
'name' => 'Book',
@@ -185,28 +297,28 @@ $query = new Google_Service_Datastore_Query([
]);
// build the request and response
-$request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]);
+$request = new Google\Service\Datastore\RunQueryRequest(['query' => $query]);
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
```
-However, as each property of the JSON API has a corresponding generated class, the above code could also be written lile this:
+However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this:
```php
// create the datastore service class
-$datastore = new Google_Service_Datastore($client)
+$datastore = new Google\Service\Datastore($client);
// build the query
-$request = new Google_Service_Datastore_RunQueryRequest();
-$query = new Google_Service_Datastore_Query();
+$request = new Google\Service\Datastore_RunQueryRequest();
+$query = new Google\Service\Datastore\Query();
// - set the order
-$order = new Google_Service_Datastore_PropertyOrder();
+$order = new Google\Service\Datastore_PropertyOrder();
$order->setDirection('descending');
-$property = new Google_Service_Datastore_PropertyReference();
+$property = new Google\Service\Datastore\PropertyReference();
$property->setName('title');
$order->setProperty($property);
$query->setOrder([$order]);
// - set the kinds
-$kind = new Google_Service_Datastore_KindExpression();
+$kind = new Google\Service\Datastore\KindExpression();
$kind->setName('Book');
$query->setKinds([$kind]);
// - set the limit
@@ -223,11 +335,13 @@ The method used is a matter of preference, but *it will be very difficult to use
If Google Authentication is desired for external applications, or a Google API is not available yet in this library, HTTP requests can be made directly.
+If you are installing this client only to authenticate your own HTTP client requests, you should use [`google/auth`](https://github.com/googleapis/google-auth-library-php#call-the-apis) instead.
+
The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization.
```php
// create the Google client
-$client = new Google_Client();
+$client = new Google\Client();
/**
* Set your method for authentication. Depending on the API, This could be
@@ -235,7 +349,7 @@ $client = new Google_Client();
* Application Default Credentials.
*/
$client->useApplicationDefaultCredentials();
-$client->addScope(Google_Service_Plus::PLUS_ME);
+$client->addScope(Google\Service\Plus::PLUS_ME);
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
@@ -246,25 +360,32 @@ $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me');
### Caching ###
-It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible library to the client:
+It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible library to the client:
```php
-$cache = new Stash\Pool(new Stash\Driver\FileSystem);
+use League\Flysystem\Adapter\Local;
+use League\Flysystem\Filesystem;
+use Cache\Adapter\Filesystem\FilesystemCachePool;
+
+$filesystemAdapter = new Local(__DIR__.'/');
+$filesystem = new Filesystem($filesystemAdapter);
+
+$cache = new FilesystemCachePool($filesystem);
$client->setCache($cache);
```
-In this example we use [StashPHP](http://www.stashphp.com/). Add this to your project with composer:
+In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer:
```
-composer require tedivm/stash
+composer require cache/filesystem-adapter
```
### Updating Tokens ###
-When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#refresh) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client:
+When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client:
```php
-$logger = new Monolog\Logger;
+$logger = new Monolog\Logger();
$tokenCallback = function ($cacheKey, $accessToken) use ($logger) {
$logger->debug(sprintf('new access token received at cache key %s', $cacheKey));
};
@@ -282,7 +403,7 @@ $httpClient = new GuzzleHttp\Client([
'verify' => false, // otherwise HTTPS requests will fail.
]);
-$client = new Google_Client();
+$client = new Google\Client();
$client->setHttpClient($httpClient);
```
@@ -290,39 +411,78 @@ Now all calls made by this library will appear in the Charles UI.
One additional step is required in Charles to view SSL requests. Go to **Charles > Proxy > SSL Proxying Settings** and add the domain you'd like captured. In the case of the Google APIs, this is usually `*.googleapis.com`.
+### Controlling HTTP Client Configuration Directly
+
+Google API Client uses [Guzzle](http://docs.guzzlephp.org/) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle.
+
+Let's say, for instance, we wished to apply a referrer to each request.
+
+```php
+use GuzzleHttp\Client;
+
+$httpClient = new Client([
+ 'headers' => [
+ 'referer' => 'mysite.com'
+ ]
+]);
+
+$client = new Google\Client();
+$client->setHttpClient($httpClient);
+```
+
+Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control.
+
+### Partial Consent and Granted Scopes
+
+When using OAuth2 3LO (e.g. you're a client requesting credentials from a 3rd
+party, such as in the [simple file upload example](examples/simple-file-upload.php)),
+you may want to take advantage of Partial Consent.
+
+To allow clients to only grant certain scopes in the OAuth2 screen, pass the
+querystring parameter for `enable_serial_consent` when generating the
+authorization URL:
+
+```php
+$authUrl = $client->createAuthUrl($scope, ['enable_serial_consent' => 'true']);
+```
+
+Once the flow is completed, you can see which scopes were granted by calling
+`getGrantedScope` on the OAuth2 object:
+
+```php
+// Space-separated string of granted scopes if it exists, otherwise null.
+echo $client->getOAuth2Service()->getGrantedScope();
+```
+
### Service Specific Examples ###
YouTube: https://github.com/youtube/api-samples/tree/master/php
-## Frequently Asked Questions ##
+## How Do I Contribute? ##
-### What do I do if something isn't working? ###
+Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the contributor license agreement.
-For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client
+## Frequently Asked Questions ##
-If there is a specific bug with the library, please [file a issue](/Google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address.
+### What do I do if something isn't working? ###
-### How do I contribute? ###
+For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: https://stackoverflow.com/questions/tagged/google-api-php-client
-We accept contributions via Github Pull Requests, but all contributors need to be covered by the standard Google Contributor License Agreement. You can find links, and more instructions, in the documentation: https://developers.google.com/api-client-library/php/contribute
+If there is a specific bug with the library, please [file an issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address.
### I want an example of X! ###
If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above!
-### Why do you still support 5.2? ###
-
-When we started working on the 1.0.0 branch we knew there were several fundamental issues to fix with the 0.6 releases of the library. At that time we looked at the usage of the library, and other related projects, and determined that there was still a large and active base of PHP 5.2 installs. You can see this in statistics such as the PHP versions chart in the WordPress stats: http://wordpress.org/about/stats/. We will keep looking at the types of usage we see, and try to take advantage of newer PHP features where possible.
-
-### Why does Google_..._Service have weird names? ###
+### Why do some Google\Service classes have weird names? ###
-The _Service classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes.
+The _Google\Service_ classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes.
### How do I deal with non-JSON response types? ###
Some services return XML or similar by default, rather than JSON, which is what the library supports. You can request a JSON response by adding an 'alt' argument to optional params that is normally the last argument to a method call:
-```
+```php
$opt_params = array(
'alt' => "json"
);
@@ -330,7 +490,7 @@ $opt_params = array(
### How do I set a field to null? ###
-The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to Google_Model::NULL_VALUE. This is a placeholder that will be replaced with a true null when sent over the wire.
+The library strips out nulls from the objects sent to the Google APIs as it is the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google\Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire.
## Code Quality ##
diff --git a/SECURITY.md b/SECURITY.md
new file mode 100644
index 000000000..8b58ae9c0
--- /dev/null
+++ b/SECURITY.md
@@ -0,0 +1,7 @@
+# Security Policy
+
+To report a security issue, please use [g.co/vulnz](https://g.co/vulnz).
+
+The Google Security Team will respond within 5 working days of your report on g.co/vulnz.
+
+We use g.co/vulnz for our intake, and do coordination and disclosure here using GitHub Security Advisory to privately discuss and fix the issue.
diff --git a/UPGRADING.md b/UPGRADING.md
index f654570a4..ef939e943 100644
--- a/UPGRADING.md
+++ b/UPGRADING.md
@@ -1,6 +1,53 @@
Google API Client Upgrade Guide
===============================
+2.x to 2.10.0
+-------------
+
+### Namespaces
+
+The Google API Client for PHP now uses namespaces for all classes. Code using
+the legacy classnames will continue to work, but it is advised to upgrade to the
+underspaced names, as the legacy classnames will be deprecated some time in the
+future.
+
+**Before**
+
+```php
+$client = new Google_Client();
+$service = new Google_Service_Books($client);
+```
+
+**After**
+```php
+$client = new Google\Client();
+$service = new Google\Service\Books($client);
+```
+
+### Service class constructors
+
+Service class constructors now accept an optional `Google\Client|array` parameter
+as their first argument, rather than requiring an instance of `Google\Client`.
+
+**Before**
+
+```php
+$client = new Google_Client();
+$client->setApplicationName("Client_Library_Examples");
+$client->setDeveloperKey("YOUR_APP_KEY");
+
+$service = new Google_Service_Books($client);
+```
+
+**After**
+
+```php
+$service = new Google\Service\Books([
+ 'application_name' => "Client_Library_Examples",
+ 'developer_key' => "YOUR_APP_KEY",
+]);
+```
+
1.0 to 2.0
----------
@@ -175,7 +222,7 @@ $client->getAuth()
**After**
```php
-$client->useDefaultApplicationCredentials();
+$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/sqlservice.admin');
```
@@ -244,7 +291,7 @@ $client->revokeToken($token);
$client->isAccessTokenExpired();
```
-## PHP 5.4 is now the minimum supported PHP version
+## PHP 5.6 is now the minimum supported PHP version
This was previously `PHP 5.2`. If you still need to use PHP 5.2, please continue to use
the [v1-master](https://github.com/google/google-api-php-client/tree/v1-master) branch.
@@ -308,20 +355,23 @@ setting the Guzzle `GuzzleHttp\ClientInterface` object.
1. Automatically refreshes access tokens if one is set and the access token is expired
- Removed `Google_Config`
- Removed `Google_Utils`
- - [`Google\Auth\CacheInterface`][Google Auth CacheInterface] is used for all caching. As a result:
+ - [`PSR-6`][PSR 6] cache is used for all caching. As a result:
1. Removed `Google_Cache_Abstract`
1. Classes `Google_Cache_Apc`, `Google_Cache_File`, `Google_Cache_Memcache`, and
`Google_Cache_Null` now implement `Google\Auth\CacheInterface`.
+ 1. Google Auth provides simple [caching utilities][Google Auth Cache] which
+ are used by default unless you provide alternatives.
- Removed `$boundary` constructor argument for `Google_Http_MediaFileUpload`
-[PSR 3]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
+[PSR 3]: https://www.php-fig.org/psr/psr-3/
+[PSR 6]: https://www.php-fig.org/psr/psr-6/
[Guzzle 5]: https://github.com/guzzle/guzzle
[Guzzle 6]: http://docs.guzzlephp.org/en/latest/psr7.html
[Monolog]: https://github.com/Seldaek/monolog
[Google Auth]: https://github.com/google/google-auth-library-php
+[Google Auth Cache]: https://github.com/googleapis/google-auth-library-php/tree/master/src/Cache
[Google Auth GCE]: https://github.com/google/google-auth-library-php/blob/master/src/GCECredentials.php
[Google Auth OAuth2]: https://github.com/google/google-auth-library-php/blob/master/src/OAuth2.php
[Google Auth Simple]: https://github.com/google/google-auth-library-php/blob/master/src/Simple.php
[Google Auth AppIdentity]: https://github.com/google/google-auth-library-php/blob/master/src/AppIdentityCredentials.php
-[Google Auth CacheInterface]: https://github.com/google/google-auth-library-php/blob/master/src/CacheInterface.php
[Firebase JWT]: https://github.com/firebase/php-jwt
diff --git a/composer.json b/composer.json
index be5bdaf1e..abacbe84b 100644
--- a/composer.json
+++ b/composer.json
@@ -6,36 +6,42 @@
"homepage": "http://developers.google.com/api-client-library/php",
"license": "Apache-2.0",
"require": {
- "php": ">=5.4",
- "google/auth": "0.9",
- "google/apiclient-services": "*@dev",
- "firebase/php-jwt": "~2.0|~3.0",
- "monolog/monolog": "^1.17",
- "phpseclib/phpseclib": "~2.0",
- "guzzlehttp/guzzle": "~5.2|~6.0",
- "guzzlehttp/psr7": "^1.2"
+ "php": "^8.0",
+ "google/auth": "^1.37",
+ "google/apiclient-services": "~0.350",
+ "firebase/php-jwt": "^6.0",
+ "monolog/monolog": "^2.9||^3.0",
+ "phpseclib/phpseclib": "^3.0.36",
+ "guzzlehttp/guzzle": "^7.4.5",
+ "guzzlehttp/psr7": "^2.6"
},
"require-dev": {
- "phpunit/phpunit": "~4",
- "squizlabs/php_codesniffer": "~2.3",
+ "squizlabs/php_codesniffer": "^3.8",
"symfony/dom-crawler": "~2.1",
"symfony/css-selector": "~2.1",
- "tedivm/stash": "^0.14.1"
+ "cache/filesystem-adapter": "^1.1",
+ "phpcompatibility/php-compatibility": "^9.2",
+ "composer/composer": "^1.10.23",
+ "phpspec/prophecy-phpunit": "^2.1",
+ "phpunit/phpunit": "^9.6"
},
"suggest": {
- "tedivm/stash": "For caching certs and tokens (using Google_Client::setCache)"
+ "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)"
},
"autoload": {
- "psr-0": {
- "Google_": "src/"
+ "psr-4": {
+ "Google\\": "src/"
},
+ "files": [
+ "src/aliases.php"
+ ],
"classmap": [
- "src/Google/Service/"
+ "src/aliases.php"
]
},
"extra": {
"branch-alias": {
- "dev-master": "2.x-dev"
+ "dev-main": "2.x-dev"
}
}
}
diff --git a/docs/README.md b/docs/README.md
new file mode 100644
index 000000000..c97236175
--- /dev/null
+++ b/docs/README.md
@@ -0,0 +1,15 @@
+# Google API Client Library for PHP Docs
+
+The Google API Client Library for PHP offers simple, flexible access to many Google APIs.
+
+## Documentation
+
+- [Getting Started](start.md)
+- [API Keys](api-keys.md)
+- [Auth](auth.md)
+- [Installation](install.md)
+- [Media](media.md)
+- [OAuth Server](oauth-server.md)
+- [OAuth Web](oauth-web.md)
+- [Pagination](pagination.md)
+- [Parameters](parameters.md)
diff --git a/docs/api-keys.md b/docs/api-keys.md
new file mode 100644
index 000000000..2e7d771e5
--- /dev/null
+++ b/docs/api-keys.md
@@ -0,0 +1,13 @@
+# API Keys
+
+When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys).
+
+> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-web.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-server.md) for more information.
+
+## Using API Keys
+
+To use API keys, call the `setDeveloperKey()` method of the `Google\Client` object before making any API calls. For example:
+
+```php
+$client->setDeveloperKey($api_key);
+```
diff --git a/docs/auth.md b/docs/auth.md
new file mode 100644
index 000000000..65dc24630
--- /dev/null
+++ b/docs/auth.md
@@ -0,0 +1,11 @@
+# Authorization
+
+The Google PHP Client Library supports several methods for making authenticated calls to the Google APIs.
+
+- [API Keys](api-keys.md)
+- [OAuth 2.0 For Webservers](oauth-web.md)
+- [OAuth 2.0 Service Accounts](oauth-server.md)
+
+In addition, it supports a method of identifying users without granting access to make Google API calls.
+
+- [ID Token Verification](id-token.md)
\ No newline at end of file
diff --git a/docs/id-token.md b/docs/id-token.md
new file mode 100644
index 000000000..56456fa88
--- /dev/null
+++ b/docs/id-token.md
@@ -0,0 +1,22 @@
+# ID Token Authentication
+
+ID tokens allow authenticating a user securely without requiring a network call (in many cases), and without granting the server access to request user information from the Google APIs.
+
+> For a complete example, see the [idtoken.php](https://github.com/googleapis/google-api-php-client/blob/master/examples/idtoken.php) sample in the examples/ directory of the client library.
+
+This is accomplished because each ID token is a cryptographically signed, base64 encoded JSON structure. The token payload includes the Google user ID, the client ID of the application the user signed in to, and the issuer (in this case, Google). It also contains a cryptographic signature which can be verified with the public Google certificates to ensure that the token was created by Google. If the user has granted permission to view their email address to the application, the ID token will additionally include their email address.
+
+The token can be easily and securely verified with the PHP client library
+
+```php
+function getUserFromToken($token) {
+ $ticket = $client->verifyIdToken($token);
+ if ($ticket) {
+ $data = $ticket->getAttributes();
+ return $data['payload']['sub']; // user ID
+ }
+ return false
+}
+```
+
+The library will automatically download and cache the certificate required for verification, and refresh it if it has expired.
diff --git a/docs/install.md b/docs/install.md
new file mode 100644
index 000000000..8218978fb
--- /dev/null
+++ b/docs/install.md
@@ -0,0 +1,39 @@
+# Installation
+
+This page contains information about installing the Google APIs Client Library for PHP.
+
+## Requirements
+
+* PHP version 5.6 or greater.
+
+## Obtaining the client library
+
+There are two options for obtaining the files for the client library.
+
+### Using Composer
+
+You can install the library by adding it as a dependency to your composer.json.
+
+```json
+"require": {
+ "google/apiclient": "^2.0"
+}
+```
+
+### Downloading from GitHub
+
+Follow [the instructions in the README](https://github.com/google/google-api-php-client#download-the-release) to download the package locally.
+
+### What to do with the files
+
+After obtaining the files, include the autoloader. If you used Composer, your require statement will look like this:
+
+```php
+require_once '/path/to/your-project/vendor/autoload.php';
+```
+
+If you downloaded the package separately, your require statement will look like this:
+
+```php
+require_once '/path/to/google-api-php-client/vendor/autoload.php';
+```
diff --git a/docs/media.md b/docs/media.md
new file mode 100644
index 000000000..6e35bb2cf
--- /dev/null
+++ b/docs/media.md
@@ -0,0 +1,75 @@
+# Media Upload
+
+The PHP client library allows for uploading large files for use with APIs such as Drive or YouTube. There are three different methods available.
+
+## Simple Upload
+
+In the simple upload case, the data is passed as the body of the request made to the server. This limits the ability to specify metadata, but is very easy to use.
+
+```php
+$file = new Google\Service\Drive\DriveFile();
+$result = $service->files->insert($file, array(
+ 'data' => file_get_contents("path/to/file"),
+ 'mimeType' => 'application/octet-stream',
+ 'uploadType' => 'media'
+));
+```
+
+## Multipart File Upload
+
+With multipart file uploads, the uploaded file is sent as one part of a multipart form post. This allows metadata about the file object to be sent as part of the post as well. This is triggered by specifying the _multipart_ uploadType.
+
+```php
+$file = new Google\Service\Drive\DriveFile();
+$file->setTitle("Hello World!");
+$result = $service->files->insert($file, array(
+ 'data' => file_get_contents("path/to/file"),
+ 'mimeType' => 'application/octet-stream',
+ 'uploadType' => 'multipart'
+));
+```
+
+## Resumable File Upload
+
+It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata.
+
+```php
+$file = new Google\Service\Drive\DriveFile();
+$file->title = "Big File";
+$chunkSizeBytes = 1 * 1024 * 1024;
+
+// Call the API with the media upload, defer so it doesn't immediately return.
+$client->setDefer(true);
+$request = $service->files->insert($file);
+
+// Create a media file upload to represent our upload process.
+$media = new Google\Http\MediaFileUpload(
+ $client,
+ $request,
+ 'text/plain',
+ null,
+ true,
+ $chunkSizeBytes
+);
+$media->setFileSize(filesize("path/to/file"));
+
+// Upload the various chunks. $status will be false until the process is
+// complete.
+$status = false;
+$handle = fopen("path/to/file", "rb");
+while (!$status && !feof($handle)) {
+ $chunk = fread($handle, $chunkSizeBytes);
+ $status = $media->nextChunk($chunk);
+ }
+
+// The final value of $status will be the data from the API for the object
+// that has been uploaded.
+$result = false;
+if($status != false) {
+ $result = $status;
+}
+
+fclose($handle);
+// Reset to the client to execute requests immediately in the future.
+$client->setDefer(false);
+```
\ No newline at end of file
diff --git a/docs/oauth-server.md b/docs/oauth-server.md
new file mode 100644
index 000000000..a4c403eb0
--- /dev/null
+++ b/docs/oauth-server.md
@@ -0,0 +1,140 @@
+# Using OAuth 2.0 for Server to Server Applications
+
+The Google APIs Client Library for PHP supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
+
+Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API.
+
+If you have a G Suite domain—if you use [G Suite Business](https://gsuite.google.com), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/google-apps/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account.
+
+> **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses.
+
+> **Note:** Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of Apps end users to share documents outside of the domain would not apply to service accounts.
+
+This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for PHP.
+
+## Overview
+
+To support server-to-server interactions, first create a service account for your project in the Developers Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account.
+
+Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server.
+
+Finally, your application can use the access token to call Google APIs.
+
+## Creating a service account
+
+A service account's credentials include a generated email address that is unique, a client ID, and at least one public/private key pair.
+
+If your application runs on Google App Engine, a service account is set up automatically when you create your project.
+
+If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following:
+
+1. Open the [**Service accounts** section](https://console.cloud.google.com/iam-admin/serviceaccounts) of the Developers Console's **IAM & Admin** page.
+2. Click **Create service account**.
+3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**.
+
+Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
+
+You can return to the [Developers Console](https://console.developers.google.com/) at any time to view the client ID, email address, and public key fingerprints, or to generate additional public/private key pairs. For more details about service account credentials in the Developers Console, see [Service accounts](https://developers.google.com/console/help/service-accounts) in the Developers Console help file.
+
+Take note of the service account's email address and store the service account's private key file in a location accessible to your application. Your application needs them to make authorized API calls.
+
+**Note:** You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys.
+
+### Delegating domain-wide authority to the service account
+
+If your application runs in a G Suite domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access.
+
+The following steps must be performed by an administrator of the G Suite domain:
+
+1. Go to your G Suite domain’s [Admin console](http://admin.google.com).
+2. Select **Security** from the list of controls. If you don't see **Security** listed, select **More controls** from the gray bar at the bottom of the page, then select **Security** from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
+3. Select **Advanced settings** from the list of options.
+4. Select **Manage third party OAuth Client access** in the **Authentication** section.
+5. In the **Client name** field enter the service account's **Client ID**.
+6. In the **One or More API Scopes** field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
+7. Click **Authorize**.
+
+Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate.
+
+[](#top_of_page)Preparing to make an authorized API call
+--------------------------------------------------------
+
+After you have obtained the client email address and private key from the Developers Console, set the path to these credentials in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable ( **Note:** This is not required in the App Engine environment):
+
+```php
+putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
+```
+
+Call the `useApplicationDefaultCredentials` to use your service account credentials to authenticate:
+
+```php
+$client = new Google\Client();
+$client->useApplicationDefaultCredentials();
+```
+
+If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method `setSubject`:
+
+```php
+$client->setSubject($user_to_impersonate);
+```
+
+Use the authorized `Google\Client` object to call Google APIs in your application.
+
+## Calling Google APIs
+
+Use the authorized `Google\Client` object to call Google APIs by completing the following steps:
+
+1. Build a service object for the API that you want to call, providing the authorized `Google\Client` object. For example, to call the Cloud SQL Administration API:
+
+ ```php
+ $sqladmin = new Google\Service\SQLAdmin($client);
+ ```
+
+2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project:
+
+ ```php
+ $response = $sqladmin->instances->listInstances('examinable-example-123')->getItems();
+ ```
+
+## Complete example
+
+The following example prints a JSON-formatted list of Cloud SQL instances in a project.
+
+To run this example:
+
+1. Create a new directory and change to it. For example:
+
+ ```sh
+ mkdir ~/php-oauth2-example
+ cd ~/php-oauth2-example
+ ```
+
+2. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org):
+
+ ```sh
+ composer require google/apiclient:^2.0
+ ```
+
+3. Create the file sqlinstances.php with the content below.
+4. Run the example from the command line:
+
+ ```
+ php ~/php-oauth2-example/sqlinstances.php
+ ```
+
+### sqlinstances.php
+
+```php
+useApplicationDefaultCredentials();
+
+$sqladmin = new Google\Service\SQLAdmin($client);
+$response = $sqladmin->instances
+ ->listInstances('examinable-example-123')->getItems();
+echo json_encode($response) . "\n";
+```
diff --git a/docs/oauth-web.md b/docs/oauth-web.md
new file mode 100644
index 000000000..904745ed5
--- /dev/null
+++ b/docs/oauth-web.md
@@ -0,0 +1,424 @@
+# Using OAuth 2.0 for Web Server Applications
+
+This document explains how web server applications use the Google API Client Library for PHP to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
+
+This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.
+
+Web server applications frequently also use [service accounts](oauth-server.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.
+
+## Prerequisites
+
+### Enable APIs for your project
+
+Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project:
+
+1. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console.
+2. Select the project associated with your application. Create a project if you do not have one already.
+3. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project.
+
+### Create authorization credentials
+
+Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
+
+1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console.
+2. Click **Create credentials > OAuth client ID**.
+3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses.
+
+ For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI.
+
+ We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page.
+
+After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access.
+
+> **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials.
+
+### Identify access scopes
+
+Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
+
+Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
+
+We also recommend that your application request access to authorization scopes via an [incremental authorization](#incremental-authorization) process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.
+
+The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document contains a full list of scopes that you might use to access Google APIs.
+
+> If your public application uses scopes that permit access to certain user data, it must pass review. If you see **unverified app** on the screen when testing your application, you must submit a verification request to remove it. Find out more about [unverified apps](https://support.google.com/cloud/answer/7454865) and get answers to [frequently asked questions about app verification](https://support.google.com/cloud/answer/9110914) in the Help Center.
+
+### Language-specific requirements
+
+To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below.
+
+To run the PHP code samples in this document, you'll need:
+
+* PHP 5.6 or greater with the command-line interface (CLI) and JSON extension installed.
+* The [Composer](https://getcomposer.org/) dependency management tool.
+* The Google APIs Client Library for PHP:
+ ```sh
+ php composer.phar require google/apiclient:^2.0
+ ```
+
+## Obtaining OAuth 2.0 access tokens
+
+The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.
+
+The list below quickly summarizes these steps:
+
+1. Your application identifies the permissions it needs.
+2. Your application redirects the user to Google along with the list of requested permissions.
+3. The user decides whether to grant the permissions to your application.
+4. Your application finds out what the user decided.
+5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf.
+
+### Step 1: Set authorization parameters
+
+Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application.
+
+The code snippet below creates a `Google\Client()` object, which defines the parameters in the authorization request.
+
+That object uses information from your **client_secret.json** file to identify your application. The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.
+
+For example, this code requests read-only, offline access to a user's Google Drive:
+
+```php
+$client = new Google\Client();
+$client->setAuthConfig('client_secret.json');
+$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
+$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
+$client->setAccessType('offline'); // offline access
+$client->setIncludeGrantedScopes(true); // incremental auth
+```
+
+The request specifies the following information:
+
+#### Parameters
+
+##### `client_id`
+
+**Required**. The client ID for your application. You can find this value in the [API Console](https://console.developers.google.com/). In PHP, call the `setAuthConfig` function to load authorization credentials from a **client_secret.json** file.
+
+```php
+$client = new Google\Client();
+$client->setAuthConfig('client_secret.json');
+```
+
+##### `redirect_uri`
+
+**Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match.
+
+To set this value in PHP, call the `setRedirectUri` function. Note that you must specify a valid redirect URI for your API Console project.
+
+```php
+$client->setRedirectUri('http://localhost:8080/oauth2callback.php');
+```
+
+##### `scope`
+
+**Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user.
+
+Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function:
+
+```php
+$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
+```
+
+The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs.
+
+We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via [incremental authorization](#Incremental-authorization), you help users to more easily understand why your application needs the access it is requesting.
+
+##### `access_type`
+
+**Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`.
+
+Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens.
+
+To set this value in PHP, call the `setAccessType` function:
+
+```php
+$client->setAccessType('offline');
+```
+
+##### `state`
+
+**Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request.
+
+You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token.
+
+To set this value in PHP, call the `setState` function:
+
+```php
+$client->setState($sample_passthrough_value);
+```
+
+##### `include_granted_scopes`
+
+**Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples.
+
+To set this value in PHP, call the `setIncludeGrantedScopes` function:
+
+```php
+$client->setIncludeGrantedScopes(true);
+```
+
+##### `login_hint`
+
+**Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session.
+
+Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID.
+
+To set this value in PHP, call the `setLoginHint` function:
+
+```php
+$client->setLoginHint('timmerman@google.com');
+```
+
+##### `prompt`
+
+**Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access.
+
+To set this value in PHP, call the `setPrompt` function:
+
+```php
+$client->setPrompt('consent');
+```
+
+Possible values are:
+
+`none`
+
+Do not display any authentication or consent screens. Must not be specified with other values.
+
+`consent`
+
+Prompt the user for consent.
+
+`select_account`
+
+Prompt the user to select an account.
+
+### Step 2: Redirect to Google's OAuth 2.0 server
+
+Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of [incremental authorization](#incremental-authorization), this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.
+
+1. Generate a URL to request access from Google's OAuth 2.0 server:
+
+ ```php
+ $auth_url = $client->createAuthUrl();
+ ```
+
+2. Redirect the user to `$auth_url`:
+
+ ```php
+ header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
+ ```
+
+Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.
+
+### Step 3: Google prompts user for consent
+
+In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials. The user can then consent or refuse to grant access to your application.
+
+Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether the access was granted. That response is explained in the following step.
+
+### Step 4: Handle the OAuth 2.0 server response
+
+The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.
+
+If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:
+
+An error response:
+
+ https://oauth2.example.com/auth?error=access_denied
+
+An authorization code response:
+
+ https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7
+
+> **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page.
+>
+> Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters.
+
+#### Sample OAuth 2.0 server response
+
+You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive:
+
+```
+https://accounts.google.com/o/oauth2/v2/auth?
+ scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
+ access_type=offline&
+ include_granted_scopes=true&
+ state=state_parameter_passthrough_value&
+ redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback&
+ response_type=code&
+ client_id=client_id
+```
+
+After completing the OAuth 2.0 flow, you should be redirected to `http://localhost/oauth2callback`, which will likely yield a `404 NOT FOUND` error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.
+
+### Step 5: Exchange authorization code for refresh and access tokens
+
+After the web server receives the authorization code, it can exchange the authorization code for an access token.
+
+To exchange an authorization code for an access token, use the `authenticate` method:
+
+```php
+$client->authenticate($_GET['code']);
+```
+
+You can retrieve the access token with the `getAccessToken` method:
+
+```php
+$access_token = $client->getAccessToken();
+```
+
+[](#top_of_page)Calling Google APIs
+-----------------------------------
+
+Use the access token to call Google APIs by completing the following steps:
+
+1. If you need to apply an access token to a new `Google\Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method:
+
+ ```php
+ $client->setAccessToken($access_token);
+ ```
+
+2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google\Client` object to the constructor for the API you want to call. For example, to call the Drive API:
+
+ ```php
+ $drive = new Google\Service\Drive($client);
+ ```
+
+3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive:
+
+ ```php
+ $files = $drive->files->listFiles(array())->getItems();
+ ```
+
+[](#top_of_page)Complete example
+--------------------------------
+
+The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files.
+
+To run this example:
+
+1. In the API Console, add the URL of the local machine to the list of redirect URLs. For example, add `http://localhost:8080`.
+2. Create a new directory and change to it. For example:
+
+ ```sh
+ mkdir ~/php-oauth2-example
+ cd ~/php-oauth2-example
+ ```
+
+3. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org):
+
+ ```sh
+ composer require google/apiclient:^2.0
+ ```
+
+4. Create the files `index.php` and `oauth2callback.php` with the content below.
+5. Run the example with a web server configured to serve PHP. If you use PHP 5.6 or newer, you can use PHP's built-in test web server:
+
+ ```sh
+ php -S localhost:8080 ~/php-oauth2-example
+ ```
+
+#### index.php
+
+```php
+setAuthConfig('client_secrets.json');
+$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
+
+if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
+ $client->setAccessToken($_SESSION['access_token']);
+ $drive = new Google\Service\Drive($client);
+ $files = $drive->files->listFiles(array())->getItems();
+ echo json_encode($files);
+} else {
+ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+}
+```
+
+#### oauth2callback.php
+
+```php
+setAuthConfigFile('client_secrets.json');
+$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
+$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
+
+if (! isset($_GET['code'])) {
+ $auth_url = $client->createAuthUrl();
+ header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
+} else {
+ $client->authenticate($_GET['code']);
+ $_SESSION['access_token'] = $client->getAccessToken();
+ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+}
+```
+
+## Incremental authorization
+
+In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission, add those scopes to your existing access token for that user.
+
+For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.
+
+In this case, at sign-in time the app might request the `profile` scope to perform basic sign-in, and then later request the `https://www.googleapis.com/auth/drive.file` scope at the time of the first request to save a mix.
+
+To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.
+
+The following rules apply to an access token obtained from an incremental authorization:
+
+* The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
+* When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of its scopes.
+* The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
+* If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.
+
+The example for [setting authorization parameters](#Step-1-Set-authorization-parameters) demonstrates how to ensure authorization requests follow this best practice. The code snippet below also shows the code that you need to add to use incremental authorization.
+
+```php
+$client->setIncludeGrantedScopes(true);
+```
+
+## Refreshing an access token (offline access)
+
+Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
+
+If you use a Google API Client Library, the [client object](#Step-1-Set-authorization-parameters) refreshes the access token as needed as long as you configure that object for offline access.
+
+Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called `online`.
+
+Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.
+
+If your application needs offline access to a Google API, set the API client's access type to `offline`:
+
+```php
+$client->setAccessType("offline");
+```
+
+After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.
+
+## Revoking a token
+
+In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting [Account Settings](https://security.google.com/settings/security/permissions). It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes or removes an application. In other words, part of the removal process can include an API request to ensure the permissions granted to the application are removed.
+
+To programmatically revoke a token, call `revokeToken()`:
+
+```php
+$client->revokeToken();
+```
+
+**Note:** Following a successful revocation response, it might take some time before the revocation has full effect.
+
+Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see our [Site Policies](https://developers.google.com/terms/site-policies). Java is a registered trademark of Oracle and/or its affiliates.
\ No newline at end of file
diff --git a/docs/pagination.md b/docs/pagination.md
new file mode 100644
index 000000000..22bf78721
--- /dev/null
+++ b/docs/pagination.md
@@ -0,0 +1,10 @@
+# Pagination
+
+Most list API calls have a maximum limit of results they will return in a single response. To allow retrieving more than this number of results, responses may return a pagination token which can be passed with a request in order to access subsequent pages.
+
+The token for the page will normally be found on list response objects, normally `nextPageToken`. This can be passed in the optional params.
+
+```php
+$token = $results->getNextPageToken();
+$server->listActivities('me', 'public', array('pageToken' => $token));
+```
\ No newline at end of file
diff --git a/docs/parameters.md b/docs/parameters.md
new file mode 100644
index 000000000..183a0c298
--- /dev/null
+++ b/docs/parameters.md
@@ -0,0 +1,14 @@
+# Standard Parameters
+
+Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google\Service\Resource` class.
+
+## Parameters
+
+- **alt**: Specify an alternative response type, for example csv.
+- **fields**: A comma separated list of fields that should be included in the response. Nested parameters can be specified with parens, e.g. key,parent(child/subsection).
+- **userIp**: The IP of the end-user making the request. This is used in per-user request quotas, as defined in the Google Developers Console
+- **quotaUser**: A user ID for the end user, an alternative to userIp for applying per-user request quotas
+- **data**: Used as part of [media](media.md)
+- **mimeType**: Used as part of [media](media.md)
+- **uploadType**: Used as part of [media](media.md)
+- **mediaUpload**: Used as part of [media](media.md)
\ No newline at end of file
diff --git a/docs/start.md b/docs/start.md
new file mode 100644
index 000000000..9c97991b7
--- /dev/null
+++ b/docs/start.md
@@ -0,0 +1,93 @@
+# Getting Started
+
+This document provides all the basic information you need to start using the library. It covers important library concepts, shows examples for various use cases, and gives links to more information.
+
+## Setup
+
+There are a few setup steps you need to complete before you can use this library:
+
+1. If you don't already have a Google account, [sign up](https://www.google.com/accounts).
+2. If you have never created a Google API project, read the [Managing Projects page](https://developers.google.com/console/help/#managingprojects) and create a project in the [Google Developers Console](https://console.developers.google.com/)
+3. [Install](install.md) the library.
+
+## Authentication and authorization
+
+It is important to understand the basics of how API authentication and authorization are handled. All API calls must use either simple or authorized access (defined below). Many API methods require authorized access, but some can use either. Some API methods that can use either behave differently, depending on whether you use simple or authorized access. See the API's method documentation to determine the appropriate access type.
+
+### 1. Simple API access (API keys)
+
+These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google Cloud project. This is needed to measure project usage for accounting purposes.
+
+#### Important concepts
+
+* **API key**: To authenticate your application, use an [API key](https://cloud.google.com/docs/authentication/api-keys) for your Google Cloud Console project. Every simple access call your application makes must include this key.
+
+> **Warning**: Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your Google Cloud project.
+
+
+### 2. Authorized API access (OAuth 2.0)
+
+These API calls access private user data. Before you can call them, the user that has access to the private data must grant your application access. Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access. All of this is accomplished with [OAuth 2.0](https://developers.google.com/identity/protocols/OAuth2) and libraries written for it.
+
+#### Important concepts
+
+* **Scope**: Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting.
+* **Refresh and access tokens**: When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token.
+
+ > **Warning**: Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data.
+
+* **Client ID and client secret**: These strings uniquely identify your application and are used to acquire tokens. They are created for your Google Cloud project on the [API Access pane](https://code.google.com/apis/console#:access) of the Google Cloud. There are three types of client IDs, so be sure to get the correct type for your application:
+
+ * Web application client IDs
+ * Installed application client IDs
+ * [Service Account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) client IDs
+
+ > **Warning**: Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Google Cloud project, and request access to user data.
+
+
+## Building and calling a service
+
+This section described how to build an API-specific service object, make calls to the service, and process the response.
+
+### Build the client object
+
+The client object is the primary container for classes and configuration in the library.
+
+```php
+$client = new Google\Client();
+$client->setApplicationName("My Application");
+$client->setDeveloperKey("MY_SIMPLE_API_KEY");
+```
+
+### Build the service object
+
+Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google\Client` to it. `Google\Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user.
+
+```php
+$service = new Google\Service\Books($client);
+```
+
+### Calling an API
+
+Each API provides resources and methods, usually in a chain. These can be accessed from the service object in the form `$service->resource->method(args)`. Most method require some arguments, then accept a final parameter of an array containing optional parameters. For example, with the Google Books API, we can make a call to list volumes matching a certain string, and add an optional _filter_ parameter.
+
+```php
+$optParams = array('filter' => 'free-ebooks');
+$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+```
+
+### Handling the result
+
+There are two main types of response - items and collections of items. Each can be accessed either as an object or as an array. Collections implement the `Iterator` interface so can be used in foreach and other constructs.
+
+```php
+foreach ($results as $item) {
+ echo $item['volumeInfo']['title'], " \n";
+}
+```
+
+**Properties are hydrated according to the value in the Response. If a property is not present in the Response it will be set to null. Some fields won't be in the Response if you didn't ask for them in the Request using the `fields` property. Therefore, watchout for null properties, maybe they have a value already but are null because they're not present in the Response.**
+
+## Google App Engine support
+
+This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API.
diff --git a/examples/README.md b/examples/README.md
index 447a93a22..b3f62d4cb 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -10,4 +10,3 @@
```
1. Point your browser to the host and port you specified, i.e `http://localhost:8000`.
-
diff --git a/examples/batch.php b/examples/batch.php
index 94118185f..3fea6085f 100644
--- a/examples/batch.php
+++ b/examples/batch.php
@@ -32,17 +32,17 @@
setDeveloperKey, the request may still succeed
using the anonymous quota.
************************************************/
-$client = new Google_Client();
+$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
if (!$apiKey = getApiKey()) {
- echo missingApiKeyWarning();
- return;
+ echo missingApiKeyWarning();
+ return;
}
$client->setDeveloperKey($apiKey);
-$service = new Google_Service_Books($client);
+$service = new Google\Service\Books($client);
/************************************************
To actually make the batch call we need to
@@ -58,30 +58,36 @@
want to execute with keys of our choice - these
keys will be reflected in the returned array.
************************************************/
-$batch = new Google_Http_Batch($client);
-$optParams = array('filter' => 'free-ebooks');
-$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+
+// NOTE: Some services use `new Google\Http\Batch($client);` instead
+$batch = $service->createBatch();
+
+$query = 'Henry David Thoreau';
+$optParams = ['filter' => 'free-ebooks'];
+$req1 = $service->volumes->listVolumes($query, $optParams);
$batch->add($req1, "thoreau");
-$req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams);
+$query = 'George Bernard Shaw';
+$req2 = $service->volumes->listVolumes($query, $optParams);
$batch->add($req2, "shaw");
/************************************************
Executing the batch will send all requests off
at once.
************************************************/
+
$results = $batch->execute();
?>
-= pageFooter(__FILE__) ?>
+= pageFooter(__FILE__);
diff --git a/examples/index.php b/examples/index.php
index 5ae2e9cf4..a55e171b6 100644
--- a/examples/index.php
+++ b/examples/index.php
@@ -1,29 +1,29 @@
-
- To view this example, run the following command from the root directory of this repository:
+
+ To view this example, run the following command from the root directory of this repository:
- php -S localhost:8080 -t examples/
+ php -S localhost:8080 -t examples/
- And then browse to "localhost:8080" in your web browser
-
+ And then browse to "localhost:8080" in your web browser
+
= pageHeader("PHP Library Examples"); ?>
-
-
-
- API Key set!
-
+
+
+
+ API Key set!
+
-
+
You have not entered your API key
-
This can be found in the Google API Console
-= pageFooter(); ?>
+= pageFooter();
diff --git a/examples/large-file-download.php b/examples/large-file-download.php
new file mode 100644
index 000000000..a3c99d0e2
--- /dev/null
+++ b/examples/large-file-download.php
@@ -0,0 +1,150 @@
+setAuthConfig($oauth_credentials);
+$client->setRedirectUri($redirect_uri);
+$client->addScope("https://www.googleapis.com/auth/drive");
+$service = new Google\Service\Drive($client);
+
+/************************************************
+ * If we have a code back from the OAuth 2.0 flow,
+ * we need to exchange that with the
+ * Google\Client::fetchAccessTokenWithAuthCode()
+ * function. We store the resultant access token
+ * bundle in the session, and redirect to ourself.
+ ************************************************/
+if (isset($_GET['code'])) {
+ $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
+ $client->setAccessToken($token);
+
+ // store in the session also
+ $_SESSION['upload_token'] = $token;
+
+ // redirect back to the example
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+}
+
+// set the access token as part of the client
+if (!empty($_SESSION['upload_token'])) {
+ $client->setAccessToken($_SESSION['upload_token']);
+ if ($client->isAccessTokenExpired()) {
+ unset($_SESSION['upload_token']);
+ }
+} else {
+ $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
+ $authUrl = $client->createAuthUrl();
+}
+
+/************************************************
+ * If we're signed in then lets try to download our
+ * file.
+ ************************************************/
+if ($client->getAccessToken()) {
+ // Check for "Big File" and include the file ID and size
+ $files = $service->files->listFiles([
+ 'q' => "name='Big File'",
+ 'fields' => 'files(id,size)'
+ ]);
+
+ if (count($files) == 0) {
+ echo "
+
+
+= pageFooter(__FILE__);
diff --git a/examples/large-file-upload.php b/examples/large-file-upload.php
index 1afda2cdd..17abdad72 100644
--- a/examples/large-file-upload.php
+++ b/examples/large-file-upload.php
@@ -24,8 +24,8 @@
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
- echo missingOAuth2CredentialsWarning();
- return;
+ echo missingOAuth2CredentialsWarning();
+ return;
}
/************************************************
@@ -34,43 +34,44 @@
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
-$client = new Google_Client();
+$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
-$service = new Google_Service_Drive($client);
+$service = new Google\Service\Drive($client);
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
- unset($_SESSION['upload_token']);
+ unset($_SESSION['upload_token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
- * Google_Client::fetchAccessTokenWithAuthCode()
+ * Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
- $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
- $client->setAccessToken($token);
+ $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
+ $client->setAccessToken($token);
- // store in the session also
- $_SESSION['upload_token'] = $token;
+ // store in the session also
+ $_SESSION['upload_token'] = $token;
- // redirect back to the example
- header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+ // redirect back to the example
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
- $client->setAccessToken($_SESSION['upload_token']);
- if ($client->isAccessTokenExpired()) {
- unset($_SESSION['upload_token']);
- }
+ $client->setAccessToken($_SESSION['upload_token']);
+ if ($client->isAccessTokenExpired()) {
+ unset($_SESSION['upload_token']);
+ }
} else {
- $authUrl = $client->createAuthUrl();
+ $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
+ $authUrl = $client->createAuthUrl();
}
/************************************************
@@ -78,69 +79,70 @@
* file.
************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
- /************************************************
- * We'll setup an empty 20MB file to upload.
- ************************************************/
- DEFINE("TESTFILE", 'testfile.txt');
- if (!file_exists(TESTFILE)) {
- $fh = fopen(TESTFILE, 'w');
- fseek($fh, 1024*1024*20);
- fwrite($fh, "!", 1);
- fclose($fh);
- }
-
- $file = new Google_Service_Drive_DriveFile();
- $file->name = "Big File";
- $chunkSizeBytes = 1 * 1024 * 1024;
-
- // Call the API with the media upload, defer so it doesn't immediately return.
- $client->setDefer(true);
- $request = $service->files->create($file);
-
- // Create a media file upload to represent our upload process.
- $media = new Google_Http_MediaFileUpload(
- $client,
- $request,
- 'text/plain',
- null,
- true,
- $chunkSizeBytes
- );
- $media->setFileSize(filesize(TESTFILE));
-
- // Upload the various chunks. $status will be false until the process is
- // complete.
- $status = false;
- $handle = fopen(TESTFILE, "rb");
- while (!$status && !feof($handle)) {
- // read until you get $chunkSizeBytes from TESTFILE
- // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
- // An example of a read buffered file is when reading from a URL
- $chunk = readVideoChunk($handle, $chunkSizeBytes);
- $status = $media->nextChunk($chunk);
- }
-
- // The final value of $status will be the data from the API for the object
- // that has been uploaded.
- $result = false;
- if ($status != false) {
- $result = $status;
- }
-
- fclose($handle);
+ /************************************************
+ * We'll setup an empty 20MB file to upload.
+ ************************************************/
+ DEFINE("TESTFILE", 'testfile.txt');
+ if (!file_exists(TESTFILE)) {
+ $fh = fopen(TESTFILE, 'w');
+ fseek($fh, 1024*1024*20);
+ fwrite($fh, "!", 1);
+ fclose($fh);
+ }
+
+ $file = new Google\Service\Drive\DriveFile();
+ $file->name = "Big File";
+ $chunkSizeBytes = 1 * 1024 * 1024;
+
+ // Call the API with the media upload, defer so it doesn't immediately return.
+ $client->setDefer(true);
+ $request = $service->files->create($file);
+
+ // Create a media file upload to represent our upload process.
+ $media = new Google\Http\MediaFileUpload(
+ $client,
+ $request,
+ 'text/plain',
+ null,
+ true,
+ $chunkSizeBytes
+ );
+ $media->setFileSize(filesize(TESTFILE));
+
+ // Upload the various chunks. $status will be false until the process is
+ // complete.
+ $status = false;
+ $handle = fopen(TESTFILE, "rb");
+ while (!$status && !feof($handle)) {
+ // read until you get $chunkSizeBytes from TESTFILE
+ // fread will never return more than 8192 bytes if the stream is read
+ // buffered and it does not represent a plain file
+ // An example of a read buffered file is when reading from a URL
+ $chunk = readVideoChunk($handle, $chunkSizeBytes);
+ $status = $media->nextChunk($chunk);
+ }
+
+ // The final value of $status will be the data from the API for the object
+ // that has been uploaded.
+ $result = false;
+ if ($status != false) {
+ $result = $status;
+ }
+
+ fclose($handle);
}
-function readVideoChunk ($handle, $chunkSize)
+function readVideoChunk($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
- // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
+ // fread will never return more than 8192 bytes if the stream is read
+ // buffered and it does not represent a plain file
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
- if ($byteCount >= $chunkSize)
- {
+ if ($byteCount >= $chunkSize) {
return $giantChunk;
}
}
@@ -149,20 +151,21 @@ function readVideoChunk ($handle, $chunkSize)
?>
-= pageFooter(__FILE__) ?>
+= pageFooter(__FILE__);
diff --git a/examples/multi-api.php b/examples/multi-api.php
index 97c33135b..e247e4139 100644
--- a/examples/multi-api.php
+++ b/examples/multi-api.php
@@ -25,8 +25,8 @@
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
- echo missingOAuth2CredentialsWarning();
- return;
+ echo missingOAuth2CredentialsWarning();
+ return;
}
/************************************************
@@ -35,7 +35,7 @@
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
-$client = new Google_Client();
+$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
@@ -43,78 +43,79 @@
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
- unset($_SESSION['multi-api-token']);
+ unset($_SESSION['multi-api-token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
- * Google_Client::fetchAccessTokenWithAuthCode()
+ * Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
- $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
- $client->setAccessToken($token);
+ $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
+ $client->setAccessToken($token);
- // store in the session also
- $_SESSION['multi-api-token'] = $token;
+ // store in the session also
+ $_SESSION['multi-api-token'] = $token;
- // redirect back to the example
- header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+ // redirect back to the example
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['multi-api-token'])) {
- $client->setAccessToken($_SESSION['multi-api-token']);
- if ($client->isAccessTokenExpired()) {
- unset($_SESSION['multi-api-token']);
- }
+ $client->setAccessToken($_SESSION['multi-api-token']);
+ if ($client->isAccessTokenExpired()) {
+ unset($_SESSION['multi-api-token']);
+ }
} else {
- $authUrl = $client->createAuthUrl();
+ $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
+ $authUrl = $client->createAuthUrl();
}
/************************************************
We are going to create both YouTube and Drive
services, and query both.
************************************************/
-$yt_service = new Google_Service_YouTube($client);
-$dr_service = new Google_Service_Drive($client);
+$yt_service = new Google\Service\YouTube($client);
+$dr_service = new Google\Service\Drive($client);
/************************************************
If we're signed in, retrieve channels from YouTube
and a list of files from Drive.
************************************************/
if ($client->getAccessToken()) {
- $_SESSION['multi-api-token'] = $client->getAccessToken();
+ $_SESSION['multi-api-token'] = $client->getAccessToken();
- $dr_results = $dr_service->files->listFiles(array('pageSize' => 10));
+ $dr_results = $dr_service->files->listFiles(['pageSize' => 10]);
- $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true));
- $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes;
- $yt_results = $yt_service->playlistItems->listPlaylistItems(
- "snippet",
- array("playlistId" => $likePlaylist)
- );
+ $yt_channels = $yt_service->channels->listChannels('contentDetails', ["mine" => true]);
+ $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes;
+ $yt_results = $yt_service->playlistItems->listPlaylistItems(
+ "snippet",
+ ["playlistId" => $likePlaylist]
+ );
}
?>
-= pageFooter(__FILE__) ?>
+= pageFooter(__FILE__);
diff --git a/examples/service-account.php b/examples/service-account.php
index 6c23f0d52..6134ae7d0 100644
--- a/examples/service-account.php
+++ b/examples/service-account.php
@@ -25,7 +25,7 @@
account.
************************************************/
-$client = new Google_Client();
+$client = new Google\Client();
/************************************************
ATTENTION: Fill in these values, or make sure you
@@ -41,32 +41,35 @@
************************************************/
if ($credentials_file = checkServiceAccountCredentialsFile()) {
- // set the location manually
- $client->setAuthConfig($credentials_file);
+ // set the location manually
+ $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
- // use the application default credentials
- $client->useApplicationDefaultCredentials();
+ // use the application default credentials
+ $client->useApplicationDefaultCredentials();
} else {
- echo missingServiceAccountDetailsWarning();
- return;
+ echo missingServiceAccountDetailsWarning();
+ return;
}
$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/books']);
-$service = new Google_Service_Books($client);
+$service = new Google\Service\Books($client);
/************************************************
We're just going to make the same call as in the
simple query as an example.
************************************************/
-$optParams = array('filter' => 'free-ebooks');
-$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+$query = 'Henry David Thoreau';
+$optParams = [
+ 'filter' => 'free-ebooks',
+];
+$results = $service->volumes->listVolumes($query, $optParams);
?>
Results Of Call:
-
- = $item['volumeInfo']['title'] ?>
+
+ = $item['volumeInfo']['title'] ?>
-
+setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
-$service = new Google_Service_Drive($client);
+$service = new Google\Service\Drive($client);
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
- unset($_SESSION['upload_token']);
+ unset($_SESSION['upload_token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
- * Google_Client::fetchAccessTokenWithAuthCode()
+ * Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
- $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
- $client->setAccessToken($token);
+ $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
+ $client->setAccessToken($token);
- // store in the session also
- $_SESSION['upload_token'] = $token;
+ // store in the session also
+ $_SESSION['upload_token'] = $token;
- // redirect back to the example
- header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
+ // redirect back to the example
+ header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
- $client->setAccessToken($_SESSION['upload_token']);
- if ($client->isAccessTokenExpired()) {
- unset($_SESSION['upload_token']);
- }
+ $client->setAccessToken($_SESSION['upload_token']);
+ if ($client->isAccessTokenExpired()) {
+ unset($_SESSION['upload_token']);
+ }
} else {
- $authUrl = $client->createAuthUrl();
+ $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
+ $authUrl = $client->createAuthUrl();
}
/************************************************
@@ -78,46 +79,46 @@
* file. For larger files, see fileupload.php.
************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
- // We'll setup an empty 1MB file to upload.
- DEFINE("TESTFILE", 'testfile-small.txt');
- if (!file_exists(TESTFILE)) {
- $fh = fopen(TESTFILE, 'w');
- fseek($fh, 1024 * 1024);
- fwrite($fh, "!", 1);
- fclose($fh);
- }
+ // We'll setup an empty 1MB file to upload.
+ DEFINE("TESTFILE", 'testfile-small.txt');
+ if (!file_exists(TESTFILE)) {
+ $fh = fopen(TESTFILE, 'w');
+ fseek($fh, 1024 * 1024);
+ fwrite($fh, "!", 1);
+ fclose($fh);
+ }
- // This is uploading a file directly, with no metadata associated.
- $file = new Google_Service_Drive_DriveFile();
- $result = $service->files->create(
- $file,
- array(
- 'data' => file_get_contents(TESTFILE),
- 'mimeType' => 'application/octet-stream',
- 'uploadType' => 'media'
- )
- );
+ // This is uploading a file directly, with no metadata associated.
+ $file = new Google\Service\Drive\DriveFile();
+ $result = $service->files->create(
+ $file,
+ [
+ 'data' => file_get_contents(TESTFILE),
+ 'mimeType' => 'application/octet-stream',
+ 'uploadType' => 'media'
+ ]
+ );
- // Now lets try and send the metadata as well using multipart!
- $file = new Google_Service_Drive_DriveFile();
- $file->setName("Hello World!");
- $result2 = $service->files->create(
- $file,
- array(
- 'data' => file_get_contents(TESTFILE),
- 'mimeType' => 'application/octet-stream',
- 'uploadType' => 'multipart'
- )
- );
+ // Now lets try and send the metadata as well using multipart!
+ $file = new Google\Service\Drive\DriveFile();
+ $file->setName("Hello World!");
+ $result2 = $service->files->create(
+ $file,
+ [
+ 'data' => file_get_contents(TESTFILE),
+ 'mimeType' => 'application/octet-stream',
+ 'uploadType' => 'multipart'
+ ]
+ );
}
?>
-= pageFooter(__FILE__) ?>
+= pageFooter(__FILE__);
diff --git a/examples/simple-query.php b/examples/simple-query.php
index 3242be5e8..61286f97e 100644
--- a/examples/simple-query.php
+++ b/examples/simple-query.php
@@ -26,17 +26,17 @@
setDeveloperKey, the request may still succeed
using the anonymous quota.
************************************************/
-$client = new Google_Client();
+$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
if (!$apiKey = getApiKey()) {
- echo missingApiKeyWarning();
- return;
+ echo missingApiKeyWarning();
+ return;
}
$client->setDeveloperKey($apiKey);
-$service = new Google_Service_Books($client);
+$service = new Google\Service\Books($client);
/************************************************
We make a call to our service, which will
@@ -47,15 +47,21 @@
(the query), and an array of named optional
parameters.
************************************************/
-$optParams = array('filter' => 'free-ebooks');
-$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+$query = 'Henry David Thoreau';
+$optParams = [
+ 'filter' => 'free-ebooks',
+];
+$results = $service->volumes->listVolumes($query, $optParams);
/************************************************
This is an example of deferring a call.
***********************************************/
$client->setDefer(true);
-$optParams = array('filter' => 'free-ebooks');
-$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
+$query = 'Henry David Thoreau';
+$optParams = [
+ 'filter' => 'free-ebooks',
+];
+$request = $service->volumes->listVolumes($query, $optParams);
$resultsDeferred = $client->execute($request);
/************************************************
@@ -64,21 +70,21 @@
array.
Some calls will return a single item which we
can immediately use. The individual responses
- are typed as Google_Service_Books_Volume, but
+ are typed as Google\Service\Books_Volume, but
can be treated as an array.
************************************************/
?>
Warning: You need download your Service Account Credentials JSON from the
Google API console.
@@ -81,12 +81,12 @@ function missingServiceAccountDetailsWarning()
as the path to this file, but in the context of this example we will do this for you.
";
- return $ret;
+ return $ret;
}
function missingOAuth2CredentialsWarning()
{
- $ret = "
+ $ret = "
Warning: You need to set the location of your OAuth2 Client Credentials from the
Google API console.
@@ -96,46 +96,72 @@ function missingOAuth2CredentialsWarning()
rename them 'oauth-credentials.json'.
";
- return $ret;
+ return $ret;
+}
+
+function invalidCsrfTokenWarning()
+{
+ $ret = "
+
+ The CSRF token is invalid, your session probably expired. Please refresh the page.
+
";
+
+ return $ret;
}
function checkServiceAccountCredentialsFile()
{
- // service account creds
- $application_creds = __DIR__ . '/../../service-account-credentials.json';
+ // service account creds
+ $application_creds = __DIR__ . '/../../service-account-credentials.json';
- return file_exists($application_creds) ? $application_creds : false;
+ return file_exists($application_creds) ? $application_creds : false;
+}
+
+function getCsrfToken()
+{
+ if (!isset($_SESSION['csrf_token'])) {
+ $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32));
+ }
+
+ return $_SESSION['csrf_token'];
+}
+
+function validateCsrfToken()
+{
+ return isset($_REQUEST['csrf_token'])
+ && isset($_SESSION['csrf_token'])
+ && $_REQUEST['csrf_token'] === $_SESSION['csrf_token'];
}
function getOAuthCredentialsFile()
{
- // oauth2 creds
- $oauth_creds = __DIR__ . '/../../oauth-credentials.json';
+ // oauth2 creds
+ $oauth_creds = __DIR__ . '/../../oauth-credentials.json';
- if (file_exists($oauth_creds)) {
- return $oauth_creds;
- }
+ if (file_exists($oauth_creds)) {
+ return $oauth_creds;
+ }
- return false;
+ return false;
}
function setClientCredentialsFile($apiKey)
{
- $file = __DIR__ . '/../../tests/.apiKey';
- file_put_contents($file, $apiKey);
+ $file = __DIR__ . '/../../tests/.apiKey';
+ file_put_contents($file, $apiKey);
}
function getApiKey()
{
- $file = __DIR__ . '/../../tests/.apiKey';
- if (file_exists($file)) {
- return file_get_contents($file);
- }
+ $file = __DIR__ . '/../../tests/.apiKey';
+ if (file_exists($file)) {
+ return file_get_contents($file);
+ }
}
function setApiKey($apiKey)
{
- $file = __DIR__ . '/../../tests/.apiKey';
- file_put_contents($file, $apiKey);
+ $file = __DIR__ . '/../../tests/.apiKey';
+ file_put_contents($file, $apiKey);
}
diff --git a/examples/url-shortener.php b/examples/url-shortener.php
deleted file mode 100644
index f9110984f..000000000
--- a/examples/url-shortener.php
+++ /dev/null
@@ -1,133 +0,0 @@
-setAuthConfig($oauth_credentials);
-$client->setRedirectUri($redirect_uri);
-$client->addScope("https://www.googleapis.com/auth/urlshortener");
-
-/************************************************
- * When we create the service here, we pass the
- * client to it. The client then queries the service
- * for the required scopes, and uses that when
- * generating the authentication URL later.
- ************************************************/
-$service = new Google_Service_Urlshortener($client);
-
-/************************************************
- * If we're logging out we just need to clear our
- * local access token in this case
- ************************************************/
-if (isset($_REQUEST['logout'])) {
- unset($_SESSION['access_token']);
-}
-
-/************************************************
- * If we have a code back from the OAuth 2.0 flow,
- * we need to exchange that with the
- * Google_Client::fetchAccessTokenWithAuthCode()
- * function. We store the resultant access token
- * bundle in the session, and redirect to ourself.
- ************************************************/
-if (isset($_GET['code'])) {
- $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
- $client->setAccessToken($token);
-
- // store in the session also
- $_SESSION['access_token'] = $token;
-
- // redirect back to the example
- header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
-}
-
-/************************************************
- If we have an access token, we can make
- requests, else we generate an authentication URL.
- ************************************************/
-if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
- $client->setAccessToken($_SESSION['access_token']);
-} else {
- $authUrl = $client->createAuthUrl();
-}
-
-/************************************************
- If we're signed in and have a request to shorten
- a URL, then we create a new URL object, set the
- unshortened URL, and call the 'insert' method on
- the 'url' resource. Note that we re-store the
- access_token bundle, just in case anything
- changed during the request - the main thing that
- might happen here is the access token itself is
- refreshed if the application has offline access.
- ************************************************/
-if ($client->getAccessToken() && isset($_GET['url'])) {
- $url = new Google_Service_Urlshortener_Url();
- $url->longUrl = $_GET['url'];
- $short = $service->url->insert($url);
- $_SESSION['access_token'] = $client->getAccessToken();
-}
-?>
-
-