make folder shared google drive api v3? - javascript

I can make a folder using this code
var request = gapi.client.request({
'path': '/drive/v3/files/',
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
//'Authorization': 'Bearer ' + token
},
'body':{
"name" : "copy",
"mimeType" : "application/vnd.google-apps.folder",
}
});
request.execute(function(resp) {
console.log(resp);
//document.getElementById("info").innerHTML = "Created folder: " + resp.title;
});
but I cant figure out for the life of me how to make the folder shared to all, I seen in the documentation to put type:anyone but I cant figure out how to do it in the code, thank you for your time

You create a permission for a file or folder with this REST function:
POST https://www.googleapis.com/drive/v3/files/fileId/permissions
So you can do:
var fileId = File Id;
var request = gapi.client.request({
'path': '/drive/v3/files/' + fileId + '/permissions',
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
//'Authorization': 'Bearer ' + token
},
'body':{
'role': 'reader', // owner, writer, commenter
'type': 'anyone'
}
});
If it's successful, it'll give you a Permissions resource as a result:
{
kind: "drive#permission",
id: Unique identifier,
type: string,
emailAddress: string,
domain: string,
role: string,
allowFileDiscovery: boolean,
displayName: string,
photoLink: string
}
I give you links to the reference, but I haven't found any example there:
Share file/folder
Create permission
Permission resource

Related

Sending media parameter is not working while sending it to kaleyra api through code

https://developers.kaleyra.io/docs/send-a-media-template-message-through-whatsapp
I am trying to send a media to kalerays api through my code. But it is not working when I pass from code. But When I hit the API from postman then it works fine.
async whatsappAPIWithAttachment(requestBody) {
let api_key = "";
if (requestBody.campaign) {
api_key = "x";
} else {
api_key = "Y";
}
var data = qs.stringify({
from: "xyz",
to: "xyz",
type: "mediatemplate",
channel: "whatsapp",
template_name: "xyz",
params: '"name","vendor"',
lang_code: "en",
media_url: "http://www.africau.edu/images/default/sample.pdf",
});
var config: AxiosRequestConfig = {
method: "post",
url: "https://api.kaleyra.io/v1/HXIN1707222213IN/messages",
headers: {
"api-key": api_key,
"content-type": "multipart/form-data",
},
data: data,
};
let response = await axios(config);
return response.data;
}
}
It gives me an enter image description hereerror request failed with status code 400. Here I have replaced X, Y, and XYZ with actual parameters. So Inputs are correct but still get an error saying 'E413', message: 'Invalid/incorrect inputs.
As per sample request in kaleyra documentation, they have used
--header 'Content-Type: application/json'
while you are passing
"content-type": "multipart/form-data",
pls correct it and try.
how about using double quote strings for param value
params: "\"name\",\"vendor\""

Rename video with Vimeo API

I'm looking to rename my videos with the Vimeo Api and Google Apps Script. I succesfully have the API moving videos into folders (using pretty much identical syntax to below) but can't for the life of me get the renaming working. It's extremely frustrating.
Here is the reference and below is my code - it just returns the video info as if I'm not trying to change anything, even though I'm clearly using a 'PATCH' call, not a 'GET'.
Where am I meant to put the 'name' parameter??
function renameVideo(){
var newName = 'thisismynewname';
var url = 'https://api.vimeo.com/videos/_________?name=' + newName;
var options = {
'method': 'PATCH',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {
'Accept':'application/vnd.vimeo.*+json;version=3.4',
'Authorization': "Bearer " + token,
},
//Note that I've also tried 'name' : 'thisismynewname' here too
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(JSON.parse(response).name); //it just returns the *current* name not the new one, and doesn't change it
}
When I saw the official document of Edit a video, it seems that name is included in the request body. So how about this modification?
Modified script:
function renameVideo(){
var newName = 'thisismynewname';
var url = 'https://api.vimeo.com/videos/_________'; // Modified
var options = {
'method': 'PATCH',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {
'Accept':'application/vnd.vimeo.*+json;version=3.4',
'Authorization': "Bearer " + token,
},
'payload': JSON.stringify({name: newName}) // Added
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(JSON.parse(response).name);
}
The content type is application/json.
Reference:
Edit a video

Using Facebook's Mobile Hosting API with Parse Cloud Code for App Links

I am having trouble getting App Links working with Parse.
Since my App is mobile only i wanted to use Facebook's Mobile Hosting API.
And since you need to send your Facebook App Secret with the request i wanted to do it with Parse Cloud Code.
All i coud find on the Facebook documentation was how to do it with cURL:
curl https://graph.facebook.com/app/app_link_hosts \
-F access_token="APP_ACCESS_TOKEN" \
-F name="iOS App Link Object Example" \
-F ios=' [
{
"url" : "sharesample://story/1234",
"app_store_id" : 12345,
"app_name" : "ShareSample",
}, ]' \
-F web=' {
"should_fallback" : false, }'
so this is what i came up with in cloud code
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://graph.facebook.com/app/app_link_hosts',
headers: {
'Content-Type': 'multipart/form-data'
},
body: {
access_token : "APP_ACCESS_TOKEN",
name : "iOS App Link Object Example",
ios : '[{"url" : "sharesample://story/1234","app_store_id" : 12345,"app_name" : "ShareSample",},]',
web : '{"should_fallback" : false,}'
}
the response i get is: Request failed with response code 400
now i just read that multipart/form-data is not supported withParse.Cloud.httpRequest
so is there another way to do this?
update: just found out that you can send multipart data with a Buffer,
so this is my code now
var Buffer = require('buffer').Buffer;
var access_token = new Buffer('APP_ACCESS_TOKEN','utf8');
var name = new Buffer('iOS App Link Object Example','utf8');
var ios = new Buffer('[{"url" : "sharesample://story/1234","app_store_id" : 12345,"app_name" : "ShareSample",},]','utf8');
var web = new Buffer('{"should_fallback" : false,}','utf8');
var contentBuffer = Buffer.concat([access_token, name, ios, web]);
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/app/app_link_hosts',
method: 'POST',
headers: {
'Content-Type': 'text/html; charset=utf-8'
},
body: contentBuffer
}
however i am still getting the same result :(
update2: got it working with content type application/x-www-form-urlencoded and normal body. But i think the error was somewhere in my parameters since i tested it with curl and got the same response
It took me a few hours, but I finally got it working:
// Returns the canonical url, like https://fb.me/....
Parse.Cloud.define("createAppLink", function(request, response) {
// see https://developers.facebook.com/docs/graph-api/reference/v2.5/app/app_link_hosts
var storyId = request.params.storyId + ''; // param identifying a single "post"
var appId = 'APP_ID';
var appSec = 'APP_SECRET';
var appToken = appId + '|' + appSec; // your app token
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/app/app_link_hosts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ // you need to stringify it
access_token: appToken,
name: 'LINK TO ' + storyId, // it is needed but not public
android: [{
url: 'app://story/' + storyId, // deep link url
package: 'com.package', // your package name
app_name: 'APP' // your app name
}],
web: { should_fallback: 'false' }
})
}).then(function(httpResponse) {
// We get an id, by which we can fetch
// the canonical url with a get request
var data = JSON.parse(httpResponse.text);
var id = data.id;
return Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/' + id,
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
params: {
access_token: appToken,
fields: 'canonical_url',
pretty: 'true'
}
});
}).then(function(httpResponse) {
var data = JSON.parse(httpResponse.text);
var canonicalUrl = data.canonical_url;
response.success(canonicalUrl);
}, function(error) {
response.error(error);
})
});

getting error while trying to batch upload photos on fan page via Facebook javascript SDK

I am trying to batch upload photos via Facebook javascript API. I did reading the API docuemnt.
(https://developers.facebook.com/docs/graph-api/reference/v2.0/page/photos
https://developers.facebook.com/docs/graph-api/making-multiple-requests)
I make batch request via the following javascript function:
function batchUploadPhotoOnPage() {
var pageId = '111222333';
var pageAccessToken = 'xxxxyyyzzzzzzz';
FB.api(
'/',
'post',
{
'batch': [
{
'method': 'post',
'relative_url': pageId+'/photos',
'access_token': pageAccessToken,
'message': 'Test Upload Photos 1...'+new Date(),
'url': 'https://hn85599112.files.wordpress.com/2014/07/girl-photo.jpeg'
},
{
'method': 'post',
'relative_url': pageId+'/photos',
'access_token': pageAccessToken,
'message': 'Test Upload Photos 2...'+new Date(),
'url': 'http://www.paopaoche.net/up/2012-5/20125523231510353173.jpg'
}
]
},
function(response) {
log(response);
}
);
}
But it always response an error message:
{
"error": {
"message": "(#324) Requires upload file",
"type": "OAuthException",
"code": 324
}
}
I am struggling for this a long time, but still can't figure it out. Please help, if you know where I am wrong. Many thanks! T_T
Have a look at this JSFiddle: http://jsfiddle.net/M8SPH/
Be sure to add your app_id, page_id and page_access_token.
Basically, you got forgot that you have to add a body parameter and URL encode the actual parameters in it:
FB.api(
'/',
'POST',
{
access_token: pageAccessToken,
'batch': [
{
'method': 'POST',
'relative_url': pageId+'/photos',
'access_token': pageAccessToken,
'body': "message=Test Upload Photos 1...&url=https://hn85599112.files.wordpress.com/2014/07/girl-photo.jpeg"
},
{
'method': 'POST',
'relative_url': pageId+'/photos',
'access_token': pageAccessToken,
'body': "message=Test Upload Photos 2...&url=http://www.paopaoche.net/up/2012-5/20125523231510353173.jpg"
}
]
},
function(response) {
console.log(JSON.stringify(response));
}
);
See https://developers.facebook.com/docs/graph-api/making-multiple-requests#multiple_methods

How to create a folder on Google Drive using javascript

Right now I'm using this code to upload files to Google Drive:
https://stackoverflow.com/a/11657773/1715263
It works fine with a textfile.
With the same code I'm trying to create a folder, using this information from Google:
https://developers.google.com/drive/folder
so Google says "Content-Type: application/json" goes into the header and "application/vnd.google-apps.folder" should be the mimetype in the body(?), thats what I'm doing in my code, which looks like this now:
function createFolder()
{
var access_token = googleAuth.getAccessToken();
var json = JSON.stringify({
mimeType: 'application/vnd.google-apps.folder',
title: 'Folder',
});
var body = "Content-Type: application/json" + "\r\n" +
"Content-Length: " + json.length + "\r\n" + "\r\n" +
json;
gapi.client.request({
'path': '/upload/drive/v2/files/',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token,
},
'body': body
}).execute(function(file) {
document.getElementById("info").innerHTML = "Created folder: " + file;
});
But it's only creating a file named "Untitled", it's no folder and you can't open it.
When I change the "Content-Type" in the "headers" section to "application/vnd.google-apps.folder" and remove the "body" part, it's creating a folder named "Untitled".
How can I get it to create a folder with a specific title?
Finally got it working by googling Claudios code which led me to this piece of code: https://stackoverflow.com/a/11361392/1715263
The important thing that changed is the 'path', its now "/drive/v2/files/" instead of "/upload/drive/v2/files/".
I just removed the 'gapi.client.load'-function, added headers information and changed the bodys mimeType.
So here's the code:
function createFolder() {
var access_token = googleAuth.getAccessToken();
var request = gapi.client.request({
'path': '/drive/v2/files/',
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token,
},
'body':{
"title" : "Folder",
"mimeType" : "application/vnd.google-apps.folder",
}
});
request.execute(function(resp) {
console.log(resp);
document.getElementById("info").innerHTML = "Created folder: " + resp.title;
});
}
Try the following code:
function createFolder(folderName) {
var body = {
'title': folderName,
'mimeType': "application/vnd.google-apps.folder"
};
var request = gapi.client.drive.files.insert({
'resource': body
});
request.execute(function(resp) {
console.log('Folder ID: ' + resp.id);
});
}

Categories

Resources