I am making a website in which I am fetching contents from dropbox and google drive.
I have got the api for dropbox :
<?php
/* Please supply your own consumer key and consumer secret */
$consumerKey = '';
$consumerSecret = '';
include 'Dropbox/autoload.php';
session_start();
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
// If the PHP OAuth extension is not available, you can try
// PEAR's HTTP_OAUTH instead.
// $oauth = new Dropbox_OAuth_PEAR($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
?>
but I have not got the api for GOOGLE DRIVE in php.
Please help me through this so that I can get the API of google drive in php and with the help of which I can fetch the contents of google drive stored by a user.
Google Drive does have an API. See: What Can You Do with the Drive Platform?
A quickstart to get it to work with PHP: Quickstart: Run a Drive App in PHP. It includes several code samples.
Related
I am trying to connect with Zoho CRM using PHP. I followed PHP SDK for Zoho CRM and installed the package.
<?php
require 'vendor/autoload.php';
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\oauth\ZohoOAuth;
$configuration =array("client_id"=>"clientid","client_secret"=>"clientsecret","redirect_uri"=>"redirecturl","currentUserEmail"=>"useremail");
$a = ZCRMRestClient::initialize($configuration);
$oAuthClient = ZohoOAuth::getClientInstance();
$refreshToken = "refreshtoken";
$userIdentifier = "emailid";
$oAuthTokens = $oAuthClient->generateAccessTokenFromRefreshToken($refreshToken,$userIdentifier);
$result = ZCRMRestClient::getModule("Contacts");
print_r($result);
exit;
?>
error I am getting:
Not able to get access token from refresh token, invalid client_id.
But I am using correct credentials to connect Zoho API.
Spent a day on this myself when I set it up.
Make sure that you are using the same domain for the api and the oauth client app.
They have 2 domains:
https://accounts.zoho.com
https://accounts.zoho.eu
If you created the oauth clinet app on one, use the same one for the api endpoint.
Further to this, if you are using accounts_url=https://accounts.zoho.eu in the oauth_configuration.properties file, then you should also set apiBaseUrl=www.zohoapis.eu in the configuration.properties file.
Given the parameters are read from these two properties files, I don't think you need that configuration array.
I want to fetch all the photos on Google photos on my web site using php.
Is it possible?. I know Picasa Web Albums Data API deprecated. I have got try it from Picasa. but i am not able to download library from https://developers.google.com/gdata/articles/php_client_lib.
There is currently no Google Photos API. The only thing available is Picasa. You may be able to upload the pictures to your google drive account and display them on your website that way. However your probably going to have to set the pictures to public.
There is an API now for Google photo's.
But I've not been successfull in making it work myself
https://developers.google.com/photos/
I am trying to do the same thing, so far, I setup the api:
From Google Console API --> enabled the photos library api.
Following this example : https://github.com/google/google-api-php-client/blob/master/examples/simple-query.php
I managed to setup the api with the following code :
include_once __DIR__ . '/vendor/autoload.php';
include_once 'base.php';
# create client
$client = new Google_Client();
$client -> setApplicationName("Client_Library_examples");
if(!$apiKey = getApiKey()) {
echo missingApiKeyWarning();
}
$client -> setDeveloperKey($apiKey);
The autoload and base.php files were copied from the mentioned link. I copied my api to a file .apiKey.
Up to this point, the code works fine, the example in the previous link explains how to create a new google service for e-books. There must be a similar thing for google photos but couldn't find any yet.
I found the following but I am not getting anything with the echo :
$response = file_get_contents('https://photoslibrary.googleapis.com/v1/albums');
$response = json_decode($response);
echo $response
I'm trying to use the Google API example to get OAuth2.0 working with my service account, but I'm having trouble with the sample code working without using the Google_Auth_AssertionCredentials since it's been deprecated.
The file is a JSON generated via the Service account page in the developer console.
Here's the snippet of the code.
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("myApp");
$client->setAuthConfig($key_file_location);
$client->setScopes(Google_Service_Analytics::ANALYTICS);
if($client->isAccessTokenExpired()) {
$client->getRefreshToken();
}
$analytics = new Google_Service_Analytics($client);
Thanks in advance for the help.
I've been taking a look at the Google API PHP Client and would like to use it to add rows to a Google Sheet. From the code, it looks like one would use this method:
public function insert($fileId, Google_Service_Drive_Property $postBody, $optParams = array())
{
$params = array('fileId' => $fileId, 'postBody' => $postBody);
$params = array_merge($params, $optParams);
return $this->call('insert', array($params), "Google_Service_Drive_Property");
}
but I can't really tell what the parameters would be. Am I heading in the right direction? Also, not quite sure on how to connect to a specific Sheet. Please advise.
Thanks!
Use Google sheets class from zend framework 1.12. They have very nicely coded library for Google Spreadsheets
https://github.com/zendframework/zf1/tree/master/library/Zend/Gdata/Spreadsheets
I figured out how to work this and wanted to share with you guys. As I stated in a comment, I did not think using Zend's GData class was a good way for me since it's very dependent on other classes throughout the framework, thus being too heavy.
So I ended up using this Spreadsheet Client on top of Google's API. Google's API is used to authenticate my service, then I start calling the Spreadsheet Client library afterwards.
After spending over a day of Googling for various problems I had for the authentication process, here's what I did to make things work:
Created a new project for Google API here
Clicked "APIs" menu on the left side under "APIs & Auth"
Searched the Drive API and enabled it (can't remember if it was necessary)
Clicked the "Credentials" menu on the left
Clicked "Create new Client ID" button under OAuth
Selected "Service Account"
After info showed & json downloaded (not needed), I clicked "Generate new P12 Key" button
I saved the p12 file somewhere I could access it through PHP
Then in the code, I added the following lines:
$email = 'somethingsomethingblahblah#developer.gserviceaccount.com';
$CLIENT_ID = $email;
$SERVICE_ACCOUNT_NAME = $email;
$KEY_FILE = 'path/to/p12/file';
$SPREADSHEETS_SCOPE = 'https://spreadsheets.google.com/feeds';
$key = file_get_contents($KEY_FILE);
$auth = new Google_Auth_AssertionCredentials(
$SERVICE_ACCOUNT_NAME,
array($SPREADSHEETS_SCOPE),
$key
);
$client = new Google_Client();
$client->setScopes(array($SPREADSHEETS_SCOPE));
$client->setAssertionCredentials($auth);
$client->getAuth()->refreshTokenWithAssertion();
$client->setClientId($CLIENT_ID);
$accessToken = $client->getAccessToken();
Also, I had to make sure I:
Shared my spreadsheet specifically with the email address on my service account in the code above
Synced my server's time (I'm running Vagrant CentOS so it's slightly different)
I believe you can run this code with other services beyond Spreadsheets, such as Youtube, Analytics, etc., but you will need to get the correct scope link (see $SPREADSHEETS_SCOPE above). Remember, this is only when using the Service Account on the Google Console, which means you are programmatically getting data from your code. If you are looking to have others users sign in using the API, then it's different.
I am having issue with the Google Drive API, i was able to fetch the files using API But i can't download via this link. I guess, must some auth, but i have used refresh tokens to authenticate.Please see below for my code
$this->load->library('google-api-php-client/src/Google_Client');
include APPPATH . '/libraries/google-api-php-client/src/contrib/Google_DriveService.php';
// Library Files Configuration to get access token and Refresh Token
$client = new Google_Client();
$client->setAccessType('offline'); // default: offline
$client->setApplicationName('xxx'); //name of the application
$client->setClientId('yyyy'); //insert your client id
$client->setClientSecret('zzz'); //insert your client secret
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$client->refreshToken($drive_data->refreshtoken);
$client->getAccessToken();
$parameters = array();
$files = $service->files->listFiles($parameters);
foreach ($files['items'] as $key => $items)
{
Download
}
Anybody knows how to get the download url with authentication?
This is having the answer:
(Java) Download URL not working
There seem to be some changes on v2 of GDrive, instead of using "downloadUrl" you may have to use "webContentLink" for getting the download link
To get downloadUrls, you need to get the metadata of a file. You can do so by using the get method. The method will return a File Resource representation. In this resource, there is a downloadUrl property. If you're able to access the files and get the URL already, then there should be no problem with your authentication setup. There could be permission issues where you may not have access to certain drive files, but if you receive no error for it, you should be fine there too. I am not particularly familiar with PHP, but perhaps you are not downloading it correctly? Here it seems to be done differently.
I also suggest that you check out the Drive PHP Quickstart App to use as a reference.
I have bumped into the same problem today and just found a solution for my case. I hope that this helps the one or another confused PHP coder out there who also does not get a downloadUrl. I assume that you are working with the examples of the v2 API, as seen on https://developers.google.com/drive/v2/reference.
First, I have altered the head to not only access the metadata but get full access (mind the DRIVE constant):
<?php
require 'vendor/autoload.php';
const DRIVE = "https://www.googleapis.com/auth/drive";
define('APPLICATION_NAME', 'MAGOS poller');
define('CREDENTIALS_PATH', 'credentials.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(Google_Service_Drive::DRIVE)));
Then I have deleted my credentials file (credentials.json) and reran the script so it authenticated once more against gDrive and recreated the credentials file. After that
$downloadUrl = $file->getDownloadUrl();
finally worked like a charm.