Create service account
- Create a service account: https://console.developers.google.com/iam-admin/serviceaccounts/
- In options, create a key: this key is your usual client_secret.json - use it the same way
- Make the role owner for the service account (Member name = service account ID = service account email ex: thomasapp@appname-201813.iam.gserviceaccount.com
- Copy the email address of your service account = service account ID
- Simply go in your browser to the Google sheet you want to interact with
- Go to SHARE on the top right of your screen
- Go to advanced settings and share it with an email address of your service account ex: thomasapp@appname-201813.iam.gserviceaccount.com
Usage
const credentials = {
private_key: "",
client_email: "",
};
const testSheet = async () => {
const db = new Mongoogle({ dsn: "mongodb://127.0.0.1:27017", credentials });
const collection = await db.sheet(
"1ZhqrUJx_tp1MLfpHdptkW-0wAVbgXhB41Kyvak8JHfs:COMMENTS",
true
);
const list = await collection.find({}).toArray();
console.log(list);
};
const testDoc = async () => {
const db = new Mongoogle({ dsn: "mongodb://127.0.0.1:27017", credentials });
const doc = await db.doc(
"1r82ml0wf5Xv3VTxNpQzBoLMtZ5Q2153VbY7bD5focPI",
true
);
console.log(doc);
};
Scope manage
https://developers.google.com/identity/protocols/oauth2/scopes
only support sheets、docs、drive