How to Generate Google Drive File Copy in a Bulk - javascript

I want to create file copies in a bulk with one call, now I am using Google Drive Rest API for copying files.
Code:
copyFileCreation(_id, _fileId, _token) {
console.log({ _id }, { _fileId }, { _token });
fetch(`https://www.googleapis.com/drive/v2/files/${_fileId}/copy`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${_token}`,
'Accept': 'application/json'
},
body: JSON.stringify({
"parents": [{
"id": _id
}],
"title": "Untitle"
}),
}).then(res => res.json()).then(data => {
console.log('copyFileCreation_data', { data });
});
}
How can I achieve my goal?
Reference: https://developers.google.cn/drive/api/v2/reference/files/copy

Have you consulted the documentation? Did you read the part where it says
Files.copy is singular one file per call.
The Batching endpoint might be a solution but that depends upon what you qualify as one call.
Batching will let you nest multiple copy calls into a single HTTP Request, but if you are trying to get around the quota limitations this will not help you as the quota is based upon each of the calls with in the batch request.

Related

How to make a post request to Wordpress to update images for employees via custom image field using Wordpress API

I'm trying to figure out a way to update employee information on a Wordpress website. so far i have figured out how to update one user at a time by adding id on the endpoint. how can i update all users at once. I'm also using custom field via ACF
var axios = require('axios');
var data = JSON.stringify({
"acf": {
"twitter": "aaaaaaa",
//"profile_picture": null,
}
});
var config = {
method: 'post',
url: 'http://localhost:8888/sitename/wp-json/wp/v2/team/1111',
headers: {
'Authorization': 'Basic dhhdsgbhadG',
'Content-Type': 'application/json'
},
data: data
};
axios(config)
.then(function(response) {
console.log(JSON.stringify(response.data));
})
.catch(function(error) {
console.log(error);
});
This code updates team member 1111 twitter url. so 2 questions
How do I do a post request for images.
How will i make this script update all employee profile pictures at once
the images are stored in a folder with each image url using their names

How do I pass use Authorize from Paypal Orders API in my method & what should I return?

I have a pending fetch request that is waiting for the authorize method below to execute on my backend.
I need to use the authorize request details from PayPal orders api from here
and implement that in the method below. I have attempted to do that below but I'm not sure if it's correct.
async authorize(): Promise<PAR> {
var res = HTTPClient.request(
'https://api-m.sandbox.paypal.com/v2/checkout/orders',
{
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization':
'Bearer access-token',
},
body: JSON.stringify({
intent: 'AUTHORIZE',
purchase_units: [
{ amount: { currency_code: 'USD', value: '100' } },
],
}),
},
);
return (await res).responseText;
}
Sorry if I'm not able to explain it properly, please feel free to ask for anything else you may need for this. This is my first time doing something in TypeScript & I got into node very very recently too.

How to update existing fields of a document in Firestore with the REST API

I have a project with Cloud Firestore as database. Now I want to update the data in one document using the fetch method. My Cloud Firestore stucture is the following:
Logging (Collection)
[userID] (Document)
Notifications (Collection)
[notificationID] (Document)
active: "true"
type: "T1"
And I use the fetch call below:
fetch("https://firestore.googleapis.com/v1/projects/[ID]/databases/(default)/documents/Logging/[userID]
+"/Notifications/[notificationID]?updateMask.fieldPaths=active", {
method: 'PATCH',
body: JSON.stringify({
"active": "false"
}),
headers: {
Authorization: 'Bearer ' + idToken,
'Content-Type': 'application/json'
}
}).then( function(response){
console.log(response);
response.json().then(function(data){
console.log(data);
});
}).catch(error => {
console.log(error);
});
Executing the fetch method I'm running in an error with message
"Invalid JSON payload received. Unknown name "active" at 'document': Cannot find field."
How can I update the existing fields of a document of Firestore with the REST API? Can anyone help me out? I've tried a lot of different "urls" and methods, but nothing works for me.
As explained in the Firestore REST API doc, You need to pass an object of type Document in the body, as follows:
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
stringValue: 'false',
},
},
}),
}
I made the assumption that your active field is of type String (since you do "active": "false"). If it is of type Boolean, you need to use a booleanValue property instead, as follows. See this doc for more details.
{
method: 'PATCH',
body: JSON.stringify({
fields: {
active: {
booleanValue: false,
},
},
}),
}

Postman pre-request to get token doesn't work

At this time I can perform succesfully my Postman request to get my token. I'm using these parameters :
-Basic Authorization in the headers
-and this body
-
Now I would like to get this request as a pre-request script (and use an environment variable for the token).
Here is the script :
pm.sendRequest({
url: 'http://localhost:8084/oauth/token',
method: 'POST',
header: {
'Authorization':'Basic Y2xpZW50OnBhc3N3b3Jk',
'content-type': 'application/x-www-form-urlencoded'
},
data:{
'password':'secret',
'username':'admin',
'grant_type':'password'
}
}, (err, res) => pm.environment.set("token", res.json().access_token));
It doesn't work with the response : Full authentication is required to access this resource.
What is wrong?
Thanks
You could change the data section to something like this?
body:{
mode:"urlencoded",
urlencoded:[
{
key:"grant_type",
value:"password"
},
{
key:"username",
value:"admin"
},
{
key:"password",
value:"secret"
}
]
}
A great resource for pm.sendRequest() examples can be found here

fetch() always results in "type: 'opaque'". But I can use Postman on my endpoint successfully

I must be really terrible at JavaScript but I've been struggling with this for a few days and I've not made any progress.
To make a long story short, I'm trying to work with UPS's REST API. I can do it with Postman and I can do it with PowerShell without any problems. JavaScript is a completely different story and I'm getting nowhere.
I've tried both XMLHttpRequest and fetch() and I've tried so many different combinations of things I can't begin to list them all.
Below is my JS function in my web app (it's triggered onchange of a field). The JS function makes a call to an Azure Function (the Azure Function works from Postman and from PowerShell.)
function getUpsShipTime() {
var jsonBody = {
"DeliveryDate": "2017-06-06",
"ShippingCode": "GND",
"ShipFrom": {
"Address": {
"StateProvinceCode": "CA",
"CountryCode": "US",
"PostalCode": "90210"
},
},
"ShipTo": {
"Address": {
"StateProvinceCode": "FL",
"CountryCode": "US",
"PostalCode": "32830"
}
}
}
var uri = "https://MyAzureFunction.azurewebsites.net/api/HttpTriggerPowerShell1?code=MyAuthCode=="
var req = new Request(uri, {
method: 'post',
mode: 'no-cors',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(jsonBody)
});
fetch(req)
.then(function (response) {
console.log(response);
return response.blob();
}).then(function (blob) {
console.log(blob);
});
}
When the function runs I get the following:
Here's what I get from Postman:
What am I doing wrong?
You request the URL in no-cors mode, which is why opaque response is returned. Effectively, that's what you asked for.
Instead, I suggest you configuring CORS for Azure Function as described here and changing mode to cors.

Categories