public static function _connect($settings) { if ($settings['client_id'] === self::$_isConnectedClientID) { // Already connected to this account. return $settings; } set_include_path(pb_backupbuddy::plugin_path() . '/destinations/gdrive/' . PATH_SEPARATOR . get_include_path()); require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Client.php'; require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Http/MediaFileUpload.php'; require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Service/Drive.php'; $client_id = $settings['client_id']; $client_secret = $settings['client_secret']; $redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'; pb_backupbuddy::status('details', 'Connecting to Google Drive.'); self::$_client = new Google_Client(); self::$_client->setClientId($client_id); self::$_client->setClientSecret($client_secret); self::$_client->setRedirectUri($redirect_uri); self::$_client->setAccessType('offline'); // Required so that Google will use the refresh token. self::$_client->addScope("https://www.googleapis.com/auth/drive"); self::$_drive = new Google_Service_Drive(self::$_client); pb_backupbuddy::status('details', 'Setting Google Drive Access Token.'); try { pb_backupbuddy::status('details', 'TOKENS: ' . print_r($settings['tokens'], true)); self::$_client->setAccessToken($settings['tokens']); } catch (Exception $e) { pb_backupbuddy::status('error', 'Error #4839484984: Unable to set access token. Access token error details: `' . $e->getMessage() . '`.'); pb_backupbuddy::status('error', 'Error #8378327: Tokens: `' . str_replace("\t", ';', print_r($settings['tokens'], true)) . '`.'); //error_log( 'Message: ' . $e->getMessage() ); //error_log( 'Tokens: ' . print_r( $settings['tokens'], true ) ); return false; } /* try { $result = self::$_client->authenticate(); // $auth_code } catch (Exception $e) { pb_backupbuddy::alert( 'Error Authenticating: ' . $e->getMessage() . ' Please go back, check codes, and try again.' ); return false; } */ // Update tokens in settings. //$oldAccessTokens = json_decode( $settings['tokens']['refreshToken'], true ); $newAccessToken = self::$_client->getAccessToken(); //print_r( $newAccessToken ); /* $accessTokens = json_decode( $newAccessToken, true ); $accessTokens['refreshToken'] = $oldAccessTokens['refreshToken']; $settings['tokens'] = json_encode( $settings['tokens'] ); // Re-encode in JSON self::$_client->setAccessToken( $settings['tokens'] ); */ $settings['tokens'] = $newAccessToken; self::$_isConnectedClientID = $settings['client_id']; return $settings; }
private function _connect($settings) { if (true === self::$_isConnected) { // Already connected. return $settings; } set_include_path(pb_backupbuddy::plugin_path() . '/destinations/gdrive/' . PATH_SEPARATOR . get_include_path()); require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Client.php'; require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Http/MediaFileUpload.php'; require_once pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Service/Drive.php'; $client_id = $settings['client_id']; $client_secret = $settings['client_secret']; $redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'; self::$_client = new Google_Client(); self::$_client->setClientId($client_id); self::$_client->setClientSecret($client_secret); self::$_client->setRedirectUri($redirect_uri); self::$_client->setAccessType('offline'); // Required so that Google will use the refresh token. self::$_client->addScope("https://www.googleapis.com/auth/drive"); self::$_drive = new Google_Service_Drive(self::$_client); self::$_client->setAccessToken($settings['tokens']); /* try { $result = self::$_client->authenticate(); // $auth_code } catch (Exception $e) { pb_backupbuddy::alert( 'Error Authenticating: ' . $e->getMessage() . ' Please go back, check codes, and try again.' ); return false; } */ // Update tokens in settings. $oldAccessTokens = json_decode($settings['tokens']['refreshToken'], true); $newAccessToken = self::$_client->getAccessToken(); //print_r( $newAccessToken ); /* $accessTokens = json_decode( $newAccessToken, true ); $accessTokens['refreshToken'] = $oldAccessTokens['refreshToken']; $settings['tokens'] = json_encode( $settings['tokens'] ); // Re-encode in JSON self::$_client->setAccessToken( $settings['tokens'] ); */ $settings['tokens'] = $newAccessToken; self::$_isConnected = true; return $settings; }