Dropbox for Yii - php

Has anyone any idea how to tie in the dropbox php api http://code.google.com/p/dropbox-php/ in Yii. I downloaded the source files and put in ext.dropbox and then inserted the following code
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
I get the error Fatal error: Class 'CExceptionEvent' not found in *

I'm not sure about Dropbox specifically, but this is how I included SwiftMailer:
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
where the setPathOfAlias looks like:
Yii::setPathOfAlias('swift', '/var/www/lib');
(I'm using it for other apps, which is why it isn't in the Yii tree. Other libs I keep in the extensions dir and for simple ones, a basic "import" is often enough.)

Try this one:
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
Yii::setPathOfAlias('swift', '/var/www/lib');
Also there is a API libary for download in this Dropbox Lib
and also a PHP 5.3 SDK for the Dropbox REST API

Related

Error calling DELETE - (403) Insufficient permissions for this file" | Google Drive API

I have created following code. I have to download files from my Google Drive folder. The folder is shared. After downloading, I have to delete those files from the Google Drive.
I tried -
$file = $service->parents->delete($file_id,$folder_id); - It doesn't do anything, nor does it give any error.
$file = $service->files->trash($file_id); - It gives Error calling DELETE - (403) Insufficient permissions for this file" Error.
My code :
<?php
require_once "google/google-api-php-client/src/Google_Client.php";
require_once "google/google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google/google-api-php-client/src/contrib/Google_Oauth2Service.php";
require_once "google/vendor/autoload.php";
$file_id = '1bJm_cqIRVh5RaVrqVXGRL0CSYwTBlZur';
$folder_id='1gEllj4B9TCnLPe_dnl1ujX4u8smLL-Ky';
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = 'service_account_email#domain.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'GoogleDriveApi-7cd8056e9eae.p12';
function buildService() {//function for first build up service
global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL, array($DRIVE_SCOPE), $key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
function printFilesInFolder($service, $folderId) {
$pageToken = NULL;
$arrayFile=array();
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$children = $service->children->listChildren($folderId, $parameters);
$arrayFile=$children;
$pageToken = $children->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $arrayFile;
}
try {
$service = buildService();
$children=printFilesInFolder($service,$folder_id);
$myfile = fopen("D:/list.txt", "wb") or die("Unable to open file!");
foreach ($children->getItems() as $child) {
print "\r\nFile Id: " . $child->getId();
fwrite($myfile, $child->getId());
fwrite($myfile, "\r\n");
}
$file = $service->parents->delete($file_id,$folder_id);
$file = $service->files->trash($file_id);
fclose($myfile);
} catch (Exception $e) {
print "An error occurred1: " . $e->getMessage();
}
?>
Your error indicates that the user doesn't have a write access to the file, and an app is attempting to modify the particular file.
Suggested action: Report to the user that there is a need to ask for
those permissions in order to update the file. You may also want to
check user access levels in the metadata retrieved by
files.get and use that to change your UI to a read only UI.

php server google drive download file in background

I need form PHP level, in background without user interaction login on google drive and get files list. I found similar topic and code for question working, but is required handly login, where I need login from php in background.
In second post with code for possibly login witout user interaction, but I don't know where is bug.
My code PHP:
<?php
require_once 'vendor/autoload.php';
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_DriveService.php';
require_once 'src/auth/Google_AssertionCredentials.php';
$client_email = 'xxxxxx#xxxxxxxxxxxxx.xxx.gserviceaccount.com';
$private_key = file_get_contents('key.p12');
$user_to_impersonate = 'xxxxxxxxxxxxxxx#gmail.com';
$scopes = array('https://www.googleapis.com/auth/drive');
$credentials = new Google_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Drive($client);
$files = $service->files->listFiles();
echo "count files=".count($files)."<br>";
foreach( $files as $item ) {
echo "title=".$item['title']."<br>";
}
?>
Vendor directory I get from this instruction, and other files I get from this GitHub
I had problem with function Google_Auth_AssertionCredentials, PHP hasn't file with this function. I found that file src/auth/Google_AssertionCredentials.php has similar function Google_AssertionCredentials. I included Google_AssertionCredentials.php file and changed function name.
In finally I have new error:
PHP Fatal error: Cannot call constructor in
\google_drive\vendor\google\apiclient-services\src\Google\Service\Drive.php
on line 75
I don't know what doing again. I tried many other metod for login on google drive, eg. load file list GET method with API_KEY, or via json file.
As result I want get file list, download them. edit and upload.
Any sugestions?
EDIT:
I have part sucess. I found liblary this, and them work with this code:
<?php
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
session_start();
require_once '/google-api-php-client/autoload.php';
$client_email = 'xxxx#xxxxx.iam.gserviceaccount.com';
$user_to_impersonate = 'xxxxxx#gmail.com';
$private_key = file_get_contents('key.p12');
$scopes = array('https://www.googleapis.com/auth/drive');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer',
$user_to_impersonate
);
$client = new Google_Client();
if(isset($_SESSION['service_token'])==false && $_SESSION['service_token']=='') {
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credentials);
}
$_SESSION['service_token'] = $client->getAccessToken();
}
if(isset($_SESSION['service_token']) && $_SESSION['service_token']) {
$client->setAccessToken($_SESSION['service_token']);
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
$_SESSION['service_token'] = $client->getAccessToken();
}
$service = new Google_Service_Drive($client);
print_r(retrieveAllFiles($service));
}
?>
And in result I have only one file [originalFilename] => Getting started, this is PDF, but I don't see this file on Gogle dirve. On google drive I uploaded file: README.md.
I'm not sure, but maybe this file is on ` 'xxxx#xxxxx.iam.gserviceaccount.com' and script not logged to 'xxxxxx#gmail.com'?

Softlayer API PHP error Function ("setObjectFilter") is not a valid method for this service

I tried to use object filter to get valid device:
Below is the php code:
$client= SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $username, $apiKey);
$filter = new stdClass();
$filter->applicationDeliveryControllers = new stdClass();
$filter->applicationDeliveryControllers->billingItem = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id->operation = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id->operation = $bId;
$client->setObjectFilter($filter);
try {
$mask ='mask[id, name]';
$client->setObjectMask($mask);
$myInstance = $clientNetscaler->getApplicationDeliveryControllers();
} catch(exception $ex) {
}
I got the following error in my run time environment:
There was an error querying the SoftLayer API: Function
("setObjectFilter") is not a valid method for this service
The error came from line $client->setObjectFilter($filter);
Anybody has any idea of why there's such error?
For me the filter is working fine. Just in case, I copied my code. Please make sure you have PHP 5.2.3 or higher and the PHP extension for SOAP. Also try re-downloading the Softlayer PHP client https://github.com/softlayer/softlayer-api-php-client and try again.
Regards
<?php
require_once ('/SoapClient.class.php');
$apiUsername = 'set me';
$apiKey = 'set me';
$accountService = Softlayer_SoapClient::getClient('SoftLayer_Account', null,$apiUsername, $apiKey);
$bId = 51774239;
$filter = new stdClass();
$filter->applicationDeliveryControllers = new stdClass();
$filter->applicationDeliveryControllers->billingItem = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id->operation = new stdClass();
$filter->applicationDeliveryControllers->billingItem->id->operation = $bId;
$accountService->setObjectFilter($filter);
$mask ='mask[id, name]';
$accountService->setObjectMask($mask);
try {
$myInstance = $accountService->getApplicationDeliveryControllers();
print_r($myInstance);
} catch (Exception $e) {
die('Unable to executre the request. ' . $e->getMessage());
}

An error comes in the code while using HybridAuth as extension in yii framework-"undefined variable-user_profile"

I used HybridAuth extension for the purpose of signing in as facebook.
The code which is causing error in Hybrid Auth is as follows:
try
{
$path1 = Yii::getPathOfAlias('ext.HybridAuth');
require_once ($path1 . '/hybridauth-' . HybridAuthIdentity::VERSION . '/hybridauth/Hybrid/Auth.php');
$config = $path1 . '/hybridauth-' . HybridAuthIdentity::VERSION . '/hybridauth/config.php';
// initialize Hybrid_Auth with a given file
$hybridauth = new Hybrid_Auth( $config );
// try to authenticate with the selected provider
$adapter = $hybridauth->authenticate( $provider_name );
// then grab the user profile
$user_profile = $adapter->getUserProfile();
}
catch(Exception $e)
{
}

Salesforce error: Element {}item invalid at this location

i am using the below code to connect to salesforce using php
require_once ('SforcePartnerClient.php');
require_once ('SforceHeaderOptions.php');
require_once ('SforceMetadataClient.php');
$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("cniRegistration.wsdl");
$loginResult = $mySforceConnection->login("username", "password.token");
$queryOptions = new QueryOptions(200);
try {
$sObject = new stdclass();
$sObject->Name = 'Smith';
$sObject->Phone = '510-555-5555';
$sObject->fieldsToNull = NULL;
echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create($sObject, 'Account');
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
echo $e->faultstring;
}
But the above code is connect to salesforce database.
But is not executing the create commands. it's giving me the below error message
Creating the following: Element {}item invalid at this location
can any one suggest me to overcome the above problem
MAK, in your sample code SessionHeader and Endpoint setup calls are missing
$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);
after setting up those, if you still see an issue, check the namespace urn
$mySforceConnection->getNamespace
It should match targetNamespace value in your wsdl
the value of $mySforceConnection should point to the xml file of the partner.wsdl.xml.
E.g $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");
Try adding the snippet code below to reference the WSDL.
$sfdc = new SforcePartnerClient();
// create a connection using the partner wsdl
$SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");
$loginResult = false;
try {
// log in with username, password and security token if required
$loginResult = $sfdc->login($sfdcUsername, $sfdcPassword.$sfdcToken);
}
catch (Exception $e) {
global $errors;
$errors = $e->faultstring;
echo "Fatal Login Error <b>" . $errors . "</b>";
die;
}
// setup the SOAP client modify the headers
$parsedURL = parse_url($sfdc->getLocation());
define ("_SFDC_SERVER_", substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
define ("_SALESFORCE_URL_", "https://test.salesforce.com");
define ("_WS_NAME_", "WebService_WDSL_Name_Here");
define ("_WS_WSDL_", "soapclient/" . _WS_NAME_ . ".wsdl");
define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);
$urlLink = '';
try {
$client = new SoapClient(_WS_WSDL_);
$sforce_header = new SoapHeader(_WS_NAMESPACE_, "SessionHeader", array("sessionId" => $sfdc->getSessionId()));
$client->__setSoapHeaders(array($sforce_header));
} catch ( Exception $e ) {
die( 'Error<br/>' . $e->__toString() );
}
Please check the link on Tech Thought for more details on the error.

Categories