Node.js Google Drive api list files in team drive - javascript

I am trying to get a list of all files in a google team drive. The code is as follows:
drive.files.list({
auth: oauth2Client,
corpora: "teamDrive",
includeTeamDriveItems: true,
supportsTeamDrives: true,
teamDriveId: someId
}, (err, response) => {
if (err) {
console.error("The API returned an error: " + err;
return;
}
console.log(response);
});
However, this only returns files located in my own drive, and not those located in the shared drive.
I then went to files.list documentation and, using the "Try this API" box on the side, entered the exact same information (along with the same scope, https://www.googleapis.com/auth/drive), and authenticated with the same google account. This returned the files located in the team drive, as expected.
So, what am I doing wrong in my code that would cause it to not return the files in the team drive, and what changes do I have to make in order to make it work?

Found the cause and solution thanks to this issue on the github page.
Basically, the quickstart guide google provides tells you to use the package google-auth-library#0.*, which is a huge mistake. It seems as though v0.* of the google-auth-library incorrectly handles parameters that you pass, meaning they don't get included in the request. Fortunately, the googleapis package includes its own, more recent, version of google-auth-library, so you can just use that for authentication. Once that's changed, the code in my original question works as expected.

Related

Critical Deployment Error ApostropheCMS Duplicate key error in Trash folder

So I have been trying to create an embedded Instagram feed in ApostropheCMS. After struggling with an npm module using the Instagram Legacy API (Detailed here), I created an local Widget module based on this idea. It works surprisingly well.
However, when I was struggling I created a couple different "Instagram" pages and deleted them. Now when I use stagecoach to deploy, I recieve the following error:
Unhandled rejection MongoError: E11000 duplicate key error collection: ButchsBBQ.aposDocs index: slug_1 dup key: { : "/trash/instagram" }
Is there a good way for me to resolve this? How can I get rid of things that I've already trashed?
As noted in comments, the answer was provided in the apostrophe discord server. This happened when I migrated the data base improperly. Basically, I trashed the page on my local machine, migrated the database and then restored the database but before before refrashing my page,I believe I trashed the same page on the server. I ended up with two pages in the trash with the same slug key.
To solve this problem, the pages had to be dropped manually via mongo shell on both the local machine and server to proceed with deployment.

facebook graph api node.js invalid appsecret_proof

this is my first post so please go easy on me!
I am a beginning developer working with javascript and node.js. I am trying to make a basic request from a node js file to facebook's graph API. I have signed up for their developer service using my facebook account, and I have installed the node package for FB found here (https://www.npmjs.com/package/fb). It looks official enough.
Everything seems to be working, except I am getting a response to my GET request with a message saying my appsecret_proof is invalid.
Here is the code I am using (be advised the sensitive info is just keyboard mashing).
let https = require("https");
var FB = require('fb');
FB.options({
version: 'v2.11',
appId: 484592542348233,
appSecret: '389fa3ha3fukzf83a3r8a3f3aa3a3'
});
FB.setAccessToken('f8af89a3f98a3f89a3f87af8afnafmdasfasedfaskjefzev8zv9z390fz39fznabacbkcbalanaa3fla398fa3lfa3flka3flina3fk3anflka3fnalifn3laifnka3fnaelfafi3eifafnaifla3nfia3nfa3ifla');
console.log(FB.options());
FB.api('/me',
'GET',
{
"fields": "id,name"
},
function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
console.log(res.id);
console.log(res.name);
}
);
The error I am getting reads:
{ message: 'Invalid appsecret_proof provided in the API argument',
type: 'GraphMethodException',
code: 100,
fbtrace_id: 'H3pDC0OPZdK' }
I have reset my appSecret and accessToken on the developer page and tried them immediately after resetting them. I get the same error, so I don't think that stale credentials are the issue. My
console.log(FB.options())
returns an appropriate looking object that also contains a long hash for appSecretProof as expected. I have also tried this code with a number of version numbers in the options (v2.4, v2.5, v2.11, and without any version key). Facebook's documentation on this strikes me as somewhat unclear. I think I should be using v2.5 of the SDK (which the node package is meant to mimic) and making requests to v2.11 of the graph API, but ??? In any case, that wouldn't seem to explain the issue I'm having. I get a perfectly good response that says my appSecretProof is invalid when I don't specify any version number at all.
The node package for fb should be generating this appSecretProof for me, and it looks like it is doing that. My other info and syntax all seem correct according to the package documentation. What am I missing here? Thank you all so much in advance.
looks like you have required the appsecret_proof for 2 factor authorization in the advance setting in your app.
Access tokens are portable. It's possible to take an access token generated on a client by Facebook's SDK, send it to a server and then make calls from that server on behalf of the client. An access token can also be stolen by malicious software on a person's computer or a man in the middle attack. Then that access token can be used from an entirely different system that's not the client and not your server, generating spam or stealing data.
You can prevent this by adding the appsecret_proof parameter to every API call from a server and enabling the setting to require proof on all calls. This prevents bad guys from making API calls with your access tokens from their servers. If you're using the official PHP SDK, the appsecret_proof parameter is automatically added.
Please refer the below url to generate the valid appsecret_proof,and add it to each api call
https://developers.facebook.com/docs/graph-api/securing-requests
I had to deal with the same issue while working with passport-facebook-token,
I finally released that the problem had nothing to have with the logic of my codebase or the app configuration.
I had this error just because I was adding intentionally an authorization Header to the request. so if you are using postman or some other http client just make sure that the request does not contain any authorization Header.

Nodejs Google Drive API Shareable link

For my Nodejs project I'm using Google drive API for file sharing. But the problem is, the URL I'm getting from API is not a shareable link. So I have to go to the drive and switch on the sharing. Is there a way to get the shareable link or switch on the sharing with the java script code? Please give me a solution for this.
The comments were very helpful indeed, this solution is for the v3 of Google Drive and the Google Doc v1. I was using jwt auth for Service Account and the alternativeLink using Drive v2 is not sharable.
I'm assuming the auth process is known and complete. I'm writing the code in blocks so using await, promises, or callbacks is reader choice
First, you need to create a document:
google.docs({version: 'v1', auth});
docs.documents.create({title: "Your Title"}, (error, response) => {
if (error) return;
//So now you have the documentId
const {data: {documentId}} = response;
});
Now with the documentId we need to add a permission to the doc. The key is to grant permission to anyone. The doc explains anyone doesn't need an email or a user.
const resource = {"role": "reader", "type": "anyone"};
drive.permissions.create({fileId:documentId, resource: resource}, (error, result)=>{
if (error) return;
//If this work then we know the permission for public share has been created
});
Now we are almost ready, we only need the URL. Gladly the sharable URL has a stable format, so we can compose it by our selves without the need for an extra request:
https://docs.google.com/document/d/${documentId}}/view
Node.js Google Api changes quickly, in case of any confusion, I'm using "googleapis": "^38.0.0",

How to get google drive folders path

I have authenticated for google drive using passport js
Now I have access token
So I am looking for to get all folders path with their respective folder Id.
I am not aware what parameters need to set to get only folders path and folder id
I am using google-drive library for fetching lists
Here is my code
var googleDrive = require('google-drive')
var param={}
googleDrive(accessToken).files().list(params, (err, response, body) => {
//fetch folders path
})
Thanks.
Try changing
var param={}
to
var param={q: "mimeType='application/vnd.google-apps.folder' and trashed=false"}
With all due respect to the authors of the library you are using, be very careful. Either a library deal with all of Drive's subtleties (eg. following nextPageTokens) or it will cause you problems. The Drive REST API is a very well formed API, and you could just as easily access it using Fetch, which, being Promise based, makes for much cleaner code and allows you to use async await.

Google SDK returns 403 `User Rate Limit Exceeded`

I am currently trying to use the google developer SDK.
In there i am trying to utilize the google translate functionality.
The translation works fine when sending in a request using my access token obtained through the google developer console.
But using this SDK it keeps throwing a 403 User Rate Limit Exceeded
this is the code in question :
googleService.defineAction$(function translateMessage(target, message, projectId) {
const translateClient = Translate({
projectId: projectId
})
console.log(target, message)
return translateClient.translate(message, target)
.then(function handleTranslateResults(results) {
console.log('result : ', result)
return result
})
.catch(function handle(err) {
console.log('err : ', err)
})
})
I have billing enabled.
I am the only one sending requests in with only about 10 characters each time.
This is why i am confused about this, there is no possible way i could have hit the limit.
Checking my quotas and request logs confirms this.
I have also tried initializing the translate client outside of the function when the code is loaded in but this produces the same results.
I do know this question has been asked before but none of those answers provide the solution to my problem and i cannot find an explanation for it online.
So after looking through the SDK's node module, i found a readme that was different from the one google promotes on github and their examples page.
The issue resided in that the object passed to Translate({}) must also contain a vaild api key.
Without it, it will make the requests to the same url the "try this feature in browser" feature in the api manager uses. this url can only be used once or twice before being asked to register for a paid account. after that it will only return 403 "User Limit Rate exceeded".
I have made a PR to the google github where the wrong example resides.
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/pull/324

Categories