|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019 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 | +// [START drive_activity_v2_quickstart] |
| 18 | +require __DIR__ . '/vendor/autoload.php'; |
| 19 | + |
| 20 | +if (php_sapi_name() != 'cli') { |
| 21 | + throw new Exception('This application must be run on the command line.'); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns an authorized API client. |
| 26 | + * @return Google_Client the authorized client object |
| 27 | + */ |
| 28 | +function getClient() |
| 29 | +{ |
| 30 | + $client = new Google_Client(); |
| 31 | + $client->setApplicationName('Google Drive Activity API Quickstart'); |
| 32 | + $client->setScopes(Google_Service_DriveActivity::DRIVE_ACTIVITY_READONLY); |
| 33 | + $client->setAuthConfig('credentials.json'); |
| 34 | + $client->setAccessType('offline'); |
| 35 | + $client->setPrompt('select_account consent'); |
| 36 | + |
| 37 | + // Load previously authorized token from a file, if it exists. |
| 38 | + // The file token.json stores the user's access and refresh tokens, and is |
| 39 | + // created automatically when the authorization flow completes for the first |
| 40 | + // time. |
| 41 | + $tokenPath = 'token.json'; |
| 42 | + if (file_exists($tokenPath)) { |
| 43 | + $accessToken = json_decode(file_get_contents($tokenPath), true); |
| 44 | + $client->setAccessToken($accessToken); |
| 45 | + } |
| 46 | + |
| 47 | + // If there is no previous token or it's expired. |
| 48 | + if ($client->isAccessTokenExpired()) { |
| 49 | + // Refresh the token if possible, else fetch a new one. |
| 50 | + if ($client->getRefreshToken()) { |
| 51 | + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); |
| 52 | + } else { |
| 53 | + // Request authorization from the user. |
| 54 | + $authUrl = $client->createAuthUrl(); |
| 55 | + printf("Open the following link in your browser:\n%s\n", $authUrl); |
| 56 | + print 'Enter verification code: '; |
| 57 | + $authCode = trim(fgets(STDIN)); |
| 58 | + |
| 59 | + // Exchange authorization code for an access token. |
| 60 | + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); |
| 61 | + $client->setAccessToken($accessToken); |
| 62 | + |
| 63 | + // Check to see if there was an error. |
| 64 | + if (array_key_exists('error', $accessToken)) { |
| 65 | + throw new Exception(join(', ', $accessToken)); |
| 66 | + } |
| 67 | + } |
| 68 | + // Save the token to a file. |
| 69 | + if (!file_exists(dirname($tokenPath))) { |
| 70 | + mkdir(dirname($tokenPath), 0700, true); |
| 71 | + } |
| 72 | + file_put_contents($tokenPath, json_encode($client->getAccessToken())); |
| 73 | + } |
| 74 | + return $client; |
| 75 | +} |
| 76 | + |
| 77 | +// Get the API client and construct the service object. |
| 78 | +$client = getClient(); |
| 79 | +$service = new Google_Service_DriveActivity($client); |
| 80 | + |
| 81 | +// Print the recent activity in your Google Drive. |
| 82 | +$request = new Google_Service_DriveActivity_QueryDriveActivityRequest(); |
| 83 | +$request->setPageSize(10); |
| 84 | +$results = $service->activity->query($request); |
| 85 | + |
| 86 | +if (count($results->getActivities()) == 0) { |
| 87 | + print "No activity.\n"; |
| 88 | +} else { |
| 89 | + print "Recent activity:\n"; |
| 90 | + foreach ($results->getActivities() as $activity) { |
| 91 | + $time = getTimeInfo($activity); |
| 92 | + $action = getActionInfo($activity->getPrimaryActionDetail()); |
| 93 | + $actors = array_map("getActorInfo", $activity->getActors()); |
| 94 | + $targets = array_map("getTargetInfo", $activity->getTargets()); |
| 95 | + printf("%s: %s, %s, %s\n", $time, truncated($actors), $action, truncated($targets)); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Returns a string representation of the first elements in a list. |
| 100 | +function truncated($array, $limit = 2) |
| 101 | +{ |
| 102 | + $contents = implode(', ', array_slice($array, 0, $limit)); |
| 103 | + $more = count($array) > $limit ? ', ...' : ''; |
| 104 | + return sprintf('[%s%s]', $contents, $more); |
| 105 | +} |
| 106 | + |
| 107 | +// Returns the name of a set property in an object, or else "unknown". |
| 108 | +function getOneOf($obj) |
| 109 | +{ |
| 110 | + foreach ($obj as $key => $val) { |
| 111 | + return $key; |
| 112 | + } |
| 113 | + return 'unknown'; |
| 114 | +} |
| 115 | + |
| 116 | +// Returns a time associated with an activity. |
| 117 | +function getTimeInfo($activity) |
| 118 | +{ |
| 119 | + if ($activity->getTimestamp() != null) { |
| 120 | + return $activity->getTimestamp(); |
| 121 | + } |
| 122 | + if ($activity->getTimeRange() != null) { |
| 123 | + return $activity->getTimeRange()->getEndTime(); |
| 124 | + } |
| 125 | + return 'unknown'; |
| 126 | +} |
| 127 | + |
| 128 | +// Returns the type of action. |
| 129 | +function getActionInfo($actionDetail) |
| 130 | +{ |
| 131 | + return getOneOf($actionDetail); |
| 132 | +} |
| 133 | + |
| 134 | +// Returns user information, or the type of user if not a known user. |
| 135 | +function getUserInfo($user) |
| 136 | +{ |
| 137 | + if ($user->getKnownUser() != null) { |
| 138 | + $knownUser = $user->getKnownUser(); |
| 139 | + $isMe = $knownUser->getIsCurrentUser(); |
| 140 | + return $isMe ? "people/me" : $knownUser->getPersonName(); |
| 141 | + } |
| 142 | + return getOneOf($user); |
| 143 | +} |
| 144 | + |
| 145 | +// Returns actor information, or the type of actor if not a user. |
| 146 | +function getActorInfo($actor) |
| 147 | +{ |
| 148 | + if ($actor->getUser() != null) { |
| 149 | + return getUserInfo($actor->getUser()); |
| 150 | + } |
| 151 | + return getOneOf($actor); |
| 152 | +} |
| 153 | + |
| 154 | +// Returns the type of a target and an associated title. |
| 155 | +function getTargetInfo($target) |
| 156 | +{ |
| 157 | + if ($target->getDriveItem() != null) { |
| 158 | + return sprintf('driveItem:"%s"', $target->getDriveItem()->getTitle()); |
| 159 | + } |
| 160 | + if ($target->getTeamDrive() != null) { |
| 161 | + return sprintf('teamDrive:"%s"', $target->getTeamDrive()->getTitle()); |
| 162 | + } |
| 163 | + if ($target->getFileComment() != null) { |
| 164 | + $parent = $target->getFileComment()->getParent(); |
| 165 | + if ($parent != null) { |
| 166 | + return sprintf('fileComment:"%s"', $parent->getTitle()); |
| 167 | + } |
| 168 | + return 'fileComment:unknown'; |
| 169 | + } |
| 170 | + return getOneOf($target); |
| 171 | +} |
| 172 | +// [END drive_activity_v2_quickstart] |
0 commit comments