Git Product home page Git Product logo

google-drive-api-demo's Introduction

google-drive-api-demo

Simple example Electron app that uses Google's Nodejs client library for interacting with Google Drive.

Allows you to...

  • Authenticate the app using OAuth2 and consent page
  • Browse files from your Google Drive
  • View various details about the files
  • Download/export files
  • Upload and create new files to your Google Drive
  • Play private videos directly from your Google Drive
Stream Videos View Files and Details
Ask user consent Export & Download

Setup

Project Setup

  1. Run npm install in a console window.
  2. Next, run the npm script from package.json to start webpack: npm run-script watch
  3. In another window or tab, run npm start to launch the Electron app.
  4. (Optional) To package the app as a Mac OSX desktop application, you can use this: electron-packager ./google-drive-api-demo GoogleAPIDemo --version <ELECTRON VERSION> --platform=darwin --out=/path/to/desired/output/directory/ --arch=all --ignore="(node_modules|src)"

Google Drive API Setup

Before you can use Google API's, you will need to setup your developer console. Follow step #1 of the NodeJS quickstart guide: https://developers.google.com/drive/v3/web/quickstart/nodejs

Once you have obtained a CLIENT_ID and SECRET_ID, paste them here.

Gotcha's

These are a few things that I got hung up on while working on this demo. Hopefuly I can help to clarify things for others.

Using the redirect URL

The quickstart guide has some awesome example code of getting an OAuth token and making an authenticated request to the Drive API, but this example requires the user to copy and paste the application's authorization key from the browser to the console, which is not very cool. There is a caveat at the end that says:

The authorization flow in this example is designed for a command line application. For information on how to perform authorization in other contexts, see the Authorizing and Authenticating. section of the library's README.

Curious to know how to do authorization in those "other contexts," I visited the library's README. The docs basically state that after the consent page, Google will do a redirect to the provided redirect url with the auth code in the query parameters. This part stumped me, because I wasn't quite sure how to provide a redirect url that I could use to get the params. I had an Ah-ha moment when I realized that you can use localhost as that redirect url. That means that if you have an HTTP server running at the address, you can snatch up the auth code that you need. The http.createServer function in main.js is an example of this.

Careful passing around the OAuth data

I was getting an error like this when I would try to make a request to the Drive API: TypeError: authClient.request is not a function. After doing some digging, I came accross this Stack Overflow post, which seemed to address the issue that I was having. I think that it boiled down to the fact that I was trying to pass around the OAuth object from Nodejs code to client-side React code, and it was getting messed up. Though the SO question/answer is not toally related to my scenerio, I think the same issue was happening: things were getting stripped from the OAuth object. The solution was to keep it in a global store somewhere. So I just ended up keeping it as a global in main.js.

Resources

I found the following links helpful while working on this demo.

google-drive-api-demo's People

Contributors

denvaar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

eskye

google-drive-api-demo's Issues

Can i run this on linux ?

How can i run this on linux ? i have tried npm start but facing issue in it.

./node_modules/electron-prebuilt/dist/Electron.app/Contents/MacOS/GoogleAPIDemo: Exec format error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.