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; }