Google Drive API Import and Export Using Picker

Many students and bloggers are request to develop google drive api import and export using picker. So we have plan to develop google drive api, now successfully completed and share the code who looking for develop google drive api.

Generally through the API we able to get third party API services. These days most of companies are using third party API for getting extra features from someone company. Because via we able to save our time and cost. This is the major reason why we go for other third party providers like social media authentication and more.

Here we are explain how get Google Drive API Import & Export features for officially share the google files. So it’s one of the best features for share our limited files using Restful API providers. Therefore most of developers are suggest to use directly API service.

What is Google Picker?

Google Picker is a “File Open” dialog for the information stored in Google servers. With Google Picker, your users can access and upload photos, videos, maps, and documents stored in Google servers. The selection is passed back to your web page or web application for further use. Through our blog you can easily learn Android Application Development.
Google Drive API Import and Export Using Picker

Register your project

To get started using Google Picker API, you need to first create or select a project in the Google Developers Console and enable the API. Using this link guides you through the process and activates the Google Picker API automatically.

Follow the steps

1.First create one project
2.Then goto Library->Search google drive and enable api
3.Again click Library->Search google picker and enable api
4.Create credential for developerKey ->API Key -> Browser key and Get API key
5.Create credential clientId->Oauthclient->Web application->Get clientId
6.If your demos are localhost then Authorised JavaScript origins = “http://localhost” and Authorized redirect URIs =”http://localhost/googlepicker/”
7.But you have use the localhost server, the downloaded file is not exported in your server so recommend to use open URL(.com, .net, .org).

Google Drive API Import and Export Using Picker

//save the file in index.php
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Picker Example</title>
 <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
 <script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
    function onApiLoad(){
        gapi.load('auth',{'callback':onAuthApiLoad}); 
        gapi.load('picker', {'callback': onPickerApiLoad});
    }
    function onAuthApiLoad(){
        window.gapi.auth.authorize({
            'client_id':'174108344819-43c44kaf23k6f329v60ffb8tqk5b1eks.apps.googleusercontent.com',
            'scope':['https://www.googleapis.com/auth/drive'],
            'immediate': false
        },handleAuthResult);
    } 
    function onPickerApiLoad() {
                pickerApiLoaded = true;
                createPicker();
            }
    var oauthToken;
    function handleAuthResult(authResult){
        if(authResult && !authResult.error){
            oauthToken = authResult.access_token;
            createPicker();
        }
    }
    function createPicker(){    
        if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.DOCS);
        view.setMimeTypes("text/html");
        var picker = new google.picker.PickerBuilder()
            .addView(new google.picker.DocsUploadView())
            .addView(new google.picker.DocsView())                
            .setOAuthToken(oauthToken)
            .setDeveloperKey('AIzaSyD4-H2tLheUTVvXBek5-3mha1mdAowiYg4')
            .setCallback(pickerCallback)
            .build();
        picker.setVisible(true);
    }
}


    function pickerCallback(data) {
                var url = 'nothing';
                var name = 'nothing';
                if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
                    var doc = data[google.picker.Response.DOCUMENTS][0];
                    url = doc[google.picker.Document.URL];
                    name = doc.name;
                    var param = {'fileId': doc.id, 'oAuthToken': oauthToken, 'name': name}
                    console.log(param);
                    document.getElementById('result').innerHTML = "Downloading...";
                    $.post('down.php', param,
                            function (returnedData) {
                                document.getElementById('result').innerHTML = "Download completed";
                            });
                            }
                        }

</script>
  </head>
  <body>
  <button onclick="onApiLoad()">Pick From Google Drive</button>
    <div id="result"></div>
    <script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
  </body>
</html>

Now add the download code

//save the file in down.php
<?php
$param = $_REQUEST;
$oAuthToken = $param['oAuthToken'];
$fileId = $param['fileId'];
$getUrl = 'https://www.googleapis.com/drive/v2/files/' . $fileId . '?alt=media';
$authHeader = 'Authorization: Bearer ' . $oAuthToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authHeader));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
file_put_contents($param['name'], $data);
echo json_encode($error);
?>

Demo

We hope you have one idea to how to develop google drive api. If you have any doubt feel free to ask me I will clarify your queries. Apart from this you can make money through Data Entry Jobs Rs.300 per Day from home. I hope these tutorials are helps to build perfect API service with help of official google services.

Leave a Reply