Solved

Include google drive in a web application

Posted on 2014-11-04
7
347 Views
Last Modified: 2014-11-09
Hello,
  I have  a web application where users should be able to upload files . I would to like then to select the file from dropbox drive.

My application is an intranet app. Can you provide an example how to implement it.

I would like to the users to select a file from drop box  and then upload and parse the file.

Thanks for your help
0
Comment
Question by:SiemensSEN
  • 3
  • 3
7 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 40421441
Your post title is different from your post content. Going from the content, where you mention only Dropbox, here's how to proceed: https://www.dropbox.com/developers/dropins/chooser/js
0
 
LVL 4

Expert Comment

by:Ersoy Hasan
ID: 40421467
This accesses Google drive as you wrote in the title
 <script type="text/javascript">
    // function which will be executed after client.js is loaded
    function handleClientLoad() {
        //authorize yourself to the user
        // first try to authorize yourself immediately(means that you already have his authorization)
        window.gapi.auth.authorize({
            //see for drive api scopes https://developers.google.com/drive/web/scopes
            scope: 'https://www.googleapis.com/auth/drive',
            client_id: 'YOUR CLIENT ID',
            immediate: true
        }, function(authResult) {
            if (authResult && !authResult.error) {
                afterAuth(authResult);
            } else {
                // No access token could be retrieved, force the authorization flow and ask for auth
                // try again with immediate false
                window.gapi.auth.authorize({
                    scope: 'https://www.googleapis.com/auth/drive',
                    client_id: 'YOUR CLIENT ID',
                    immediate: false
                }, afterAuth);
            }


        });
    }
    function afterAuth(authResult) {
        if (authResult && !authResult.error) {
            window.gapi.client.load('drive', 'v2', function(response) {
                //and now you have your api
                //see for full https://developers.google.com/drive/v2/reference/
                var request = gapi.client.drive.files.list();
                request.execute(function(response) {
                    alert(response.items);
                });
            });
        }
    }
</script>
//onload defines which function will be loaded
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Open in new window


If you have any more questions please ask :)
0
 

Author Comment

by:SiemensSEN
ID: 40421624
Thanks for the help..

Sorry for confusion.. I need to support both Google drive and drop box.

is there any  restriction for Intranet application. I saw an article that says that in order to used Google drive that your application must be an Internet app..

Thanks for your help
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:Ersoy Hasan
ID: 40421638
Your application must have an access to internet it does not mean your application must be accessible from the net. So what I mean is you can access google api from your intranet application.
0
 

Author Comment

by:SiemensSEN
ID: 40428383
Thanks you for your help. I get the google popup dialog and I can select file but I have download it

I get this error in the "pickerCallback" function.. can help me..

TypeError: gapi.client.drive is undefined
var request = gapi.client.drive.files.get({



here my sample code

<!DOCTYPE html>
<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>
    function onApiLoad(){
        gapi.load('auth',{'callback':onAuthApiLoad}); 
        gapi.load('picker'); 
    }
    function onAuthApiLoad(){
        window.gapi.auth.authorize({
            'client_id':'585152983161-rbb77vrf6hp14fm0js0uf0skdr3vm4bd.apps.googleusercontent.com',
            'scope':['https://www.googleapis.com/auth/drive']
        },handleAuthResult);
    } 
    var oauthToken;
    function handleAuthResult(authResult){
        if(authResult && !authResult.error){
            oauthToken = authResult.access_token;
            createPicker();
        }
    }


    function createPicker(){    
        var picker = new google.picker.PickerBuilder()
            .addView(new google.picker.DocsView())                
            .setOAuthToken(oauthToken)
            .setCallback(pickerCallback)
            .build();
        picker.setVisible(true);
    }

    function pickerCallback(data) {
        var url = '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];
		  var fileId = data.docs[0].id;
		var request = gapi.client.drive.files.get({
			'fileId': fileId
		});
  request.execute(function(resp) {
    console.log('Title: ' + resp.title);
    console.log('Description: ' + resp.description);
    console.log('MIME type: ' + resp.mimeType);
  });

             

        }
        var message = 'You picked: ' + url;
        document.getElementById('result').innerHTML = message;
      }
</script>
  </head>
  <body>
    <div id="result"></div>
<a id="auth-button" class="google-drive-btn" href="#" onclick="onApiLoad()">Choose from Google Drive</a>
    <script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<script src="https://apis.google.com/js/client.js"></script>
  </body>
</html>

Open in new window

0
 
LVL 4

Accepted Solution

by:
Ersoy Hasan earned 500 total points
ID: 40428512
 if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
          var doc = data[google.picker.Response.DOCUMENTS][0];
          url = doc[google.picker.Document.URL];
		  var fileId = data.docs[0].id;
		var request = gapi.client.drive.files.get({
			'fileId': fileId
		});
  request.execute(function(resp) {
    console.log('Title: ' + resp.title);
    console.log('Description: ' + resp.description);
    console.log('MIME type: ' + resp.mimeType);
  });

             

        }
        var message = 'You picked: ' + url;
        document.getElementById('result').innerHTML = message;

Open in new window

This whole section is wrong.You cant access data this way. Here is how you should check if anything is picked:
data.action === google.picker.Action.PICKED

Open in new window

here is the data arguments structure:
{
    action: "picked",
    viewToken:["all", null, Object {}],
    docs:[ArrayofPickedDocuments]
}

Open in new window

0
 

Author Closing Comment

by:SiemensSEN
ID: 40431236
Thanks for your help

I change the function

Now I have the File URL

 I have pass it to my WepAPI to be parse and save in c# using WebClient  but I am not sure how to get the
TokenType and.AccessToken

but I will open another ticked

Thanks again for your help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now