styles
7 years ago
templates
7 years ago
appengineauth.php
7 years ago
batch.php
7 years ago
fileupload.php
7 years ago
idtoken.php
7 years ago
index.php
7 years ago
multi-api.php
7 years ago
service-account.php
7 years ago
simple-query.php
7 years ago
simplefileupload.php
7 years ago
user-example.php
7 years ago
user-example.php
133 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | include_once "templates/base.php"; |
| 18 | session_start(); |
| 19 | |
| 20 | require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 21 | |
| 22 | /************************************************ |
| 23 | ATTENTION: Fill in these values! Make sure |
| 24 | the redirect URI is to this page, e.g: |
| 25 | http://localhost:8080/user-example.php |
| 26 | ************************************************/ |
| 27 | $client_id = '<YOUR_CLIENT_ID>'; |
| 28 | $client_secret = '<YOUR_CLIENT_SECRET>'; |
| 29 | $redirect_uri = '<YOUR_REDIRECT_URI>'; |
| 30 | |
| 31 | /************************************************ |
| 32 | Make an API request on behalf of a user. In |
| 33 | this case we need to have a valid OAuth 2.0 |
| 34 | token for the user, so we need to send them |
| 35 | through a login flow. To do this we need some |
| 36 | information from our API console project. |
| 37 | ************************************************/ |
| 38 | $client = new Google_Client(); |
| 39 | $client->setClientId($client_id); |
| 40 | $client->setClientSecret($client_secret); |
| 41 | $client->setRedirectUri($redirect_uri); |
| 42 | $client->addScope("https://www.googleapis.com/auth/urlshortener"); |
| 43 | |
| 44 | /************************************************ |
| 45 | When we create the service here, we pass the |
| 46 | client to it. The client then queries the service |
| 47 | for the required scopes, and uses that when |
| 48 | generating the authentication URL later. |
| 49 | ************************************************/ |
| 50 | $service = new Google_Service_Urlshortener($client); |
| 51 | |
| 52 | /************************************************ |
| 53 | If we're logging out we just need to clear our |
| 54 | local access token in this case |
| 55 | ************************************************/ |
| 56 | if (isset($_REQUEST['logout'])) { |
| 57 | unset($_SESSION['access_token']); |
| 58 | } |
| 59 | |
| 60 | /************************************************ |
| 61 | If we have a code back from the OAuth 2.0 flow, |
| 62 | we need to exchange that with the authenticate() |
| 63 | function. We store the resultant access token |
| 64 | bundle in the session, and redirect to ourself. |
| 65 | ************************************************/ |
| 66 | if (isset($_GET['code'])) { |
| 67 | $client->authenticate($_GET['code']); |
| 68 | $_SESSION['access_token'] = $client->getAccessToken(); |
| 69 | $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 70 | header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 71 | } |
| 72 | |
| 73 | /************************************************ |
| 74 | If we have an access token, we can make |
| 75 | requests, else we generate an authentication URL. |
| 76 | ************************************************/ |
| 77 | if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { |
| 78 | $client->setAccessToken($_SESSION['access_token']); |
| 79 | } else { |
| 80 | $authUrl = $client->createAuthUrl(); |
| 81 | } |
| 82 | |
| 83 | /************************************************ |
| 84 | If we're signed in and have a request to shorten |
| 85 | a URL, then we create a new URL object, set the |
| 86 | unshortened URL, and call the 'insert' method on |
| 87 | the 'url' resource. Note that we re-store the |
| 88 | access_token bundle, just in case anything |
| 89 | changed during the request - the main thing that |
| 90 | might happen here is the access token itself is |
| 91 | refreshed if the application has offline access. |
| 92 | ************************************************/ |
| 93 | if ($client->getAccessToken() && isset($_GET['url'])) { |
| 94 | $url = new Google_Service_Urlshortener_Url(); |
| 95 | $url->longUrl = $_GET['url']; |
| 96 | $short = $service->url->insert($url); |
| 97 | $_SESSION['access_token'] = $client->getAccessToken(); |
| 98 | } |
| 99 | |
| 100 | echo pageHeader("User Query - URL Shortener"); |
| 101 | if (strpos($client_id, "googleusercontent") == false) { |
| 102 | echo missingClientSecretsWarning(); |
| 103 | exit; |
| 104 | } |
| 105 | ?> |
| 106 | <div class="box"> |
| 107 | <div class="request"> |
| 108 | <?php |
| 109 | if (isset($authUrl)) { |
| 110 | echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; |
| 111 | } else { |
| 112 | echo <<<END |
| 113 | <form id="url" method="GET" action="{$_SERVER['PHP_SELF']}"> |
| 114 | <input name="url" class="url" type="text"> |
| 115 | <input type="submit" value="Shorten"> |
| 116 | </form> |
| 117 | <a class='logout' href='?logout'>Logout</a> |
| 118 | END; |
| 119 | } |
| 120 | ?> |
| 121 | </div> |
| 122 | |
| 123 | <div class="shortened"> |
| 124 | <?php |
| 125 | if (isset($short)) { |
| 126 | var_dump($short); |
| 127 | } |
| 128 | ?> |
| 129 | </div> |
| 130 | </div> |
| 131 | <?php |
| 132 | echo pageFooter(__FILE__); |
| 133 |