Coder Social home page Coder Social logo

deno-google's Introduction

Deno-Google

Drive

An easy way to access Google Drive without any external dependencies.

import { GoogleDrive } from "https://deno.land/x.google/drive.ts";

const gd = new GoogleDrive({
  client_id: "xxxxx-xxxxxxxxxxxxxx.apps.googleusercontent.com",
  client_secret: "xxxxxxxxxxxxxxx",
  refresh_token: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  logger: false,
});

try {
  // This step can be omitted, and it will reauthorize when requesting data
  await gd.authorize();
  // The default value of the path is "root"
  const metadata = await gd.index("your/path");

  if (metadata.isFolder) {
    console.log(metadata.list());
  } else {
    // handle metadata.raw() or metadata.raw(range)
  }
} catch (e) {
  console.log(e);
}

OAuth

This class is a simplification of the google oauth2 authorization flow.

1. Create an instance

import { GoogleOAuth } from "https://deno.land/x/google/oauth.ts";

const ga = new GoogleOAuth({
  client_id: "xxxxx-xxxxxxxxxxxxxx.apps.googleusercontent.com",
  client_secret: "xxxxxxxxxxxxxxx",
  redirect_uri: "http://example.com/redirect_uri",
  "scopes": [
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/drive.readonly",
  ],
});

const link = ga.buildAuthLink();

2. Visit the link

After clicking the link to complete the authorization, it will redirect to the uri with the "code" parameter. You can receive the "code" in redirect_uri.

3. Get tokens

// tokens include refresh_token, access_token, id_token, etc.
// But the access_token has an expiry time, so you need to get it again through the next step
const tokens = await ga.getTokens(code);

4. Get access_token

// refresh_token is obtained in the previous step, it is permanently valid.
const accessToken = ga.getAccessToken(refresh_token);

5. Decode id_token (without verifiy signature)

The id_token obtained in the third step contains some personal information of the google account, you can decode it.

const data = ga.decodeIdToken(id_token);
// it will return { header, payload, signature }
// payload is the personal information

6. Decode id_token (verify signature via CERTS_URL)

It is usually not necessary to verify the signature for the first authorization, but it is strongly recommended to verify when you use the id_token as a cookie to keep the login state.

NOTE: There is a bug in this method, which is caused by the type of "cryptoKey", and I don't know how to solve it for the time being.

const data = await ga.verifyIdToken(id_token);

deno-google's People

Contributors

metadream avatar

Stargazers

 avatar

Watchers

 avatar

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.