Google Drive Api for Service account. Error while creating a file (probably due to base64)2019 Community Moderator ElectionDisplaying files (e.g. images) stored in Google Drive on a websitechange Google Drive mimeType on update fileHow to create a public Google Doc through the Drive API (PHP client)how to upload file to google drive using sdk in uploadtype=mediaOauth2 Google Drive offline access not working for non-google app files?Google Drive REST API update content as HTML with Landscape Orientation google-drive-sdkUpload Files to GOOGLE DRIVE without user authentication only when form submitted in LARAVEL OR AJAXCan not create folder using multipartPyDrive: Create a Google Doc fileGoogle Drive Uploading multipart mime base64 encoded file w/powershell: malformed multipart body error

Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?

2×2×2 rubik's cube corner is twisted!

Subset counting for even numbers

What do you call the air that rushes into your car in the highway?

Word for a person who has no opinion about whether god exists

Virginia employer terminated employee and wants signing bonus returned

Examples of a statistic that is not independent of sample's distribution?

Latest web browser compatible with Windows 98

Grey hair or white hair

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

What Happens when Passenger Refuses to Fly Boeing 737 Max?

Why does Captain Marvel assume the planet where she lands would recognize her credentials?

My story is written in English, but is set in my home country. What language should I use for the dialogue?

How does airport security verify that you can carry a battery bank over 100 Wh?

Replacing Windows 7 security updates with anti-virus?

Accountant/ lawyer will not return my call

どこ and なに in subject questions

A question on the ultrafilter number

Who deserves to be first and second author? PhD student who collected data, research associate who wrote the paper or supervisor?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

If the Captain's screens are out, does he switch seats with the co-pilot?

Am I not good enough for you?

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?



Google Drive Api for Service account. Error while creating a file (probably due to base64)



2019 Community Moderator ElectionDisplaying files (e.g. images) stored in Google Drive on a websitechange Google Drive mimeType on update fileHow to create a public Google Doc through the Drive API (PHP client)how to upload file to google drive using sdk in uploadtype=mediaOauth2 Google Drive offline access not working for non-google app files?Google Drive REST API update content as HTML with Landscape Orientation google-drive-sdkUpload Files to GOOGLE DRIVE without user authentication only when form submitted in LARAVEL OR AJAXCan not create folder using multipartPyDrive: Create a Google Doc fileGoogle Drive Uploading multipart mime base64 encoded file w/powershell: malformed multipart body error










0















I've created a folder and a file w/ permission to access a file for any Internet user. I used to think upload itself worked properly. Unfortunately, when I use




https://drive.google.com/uc?export=view&id=file.id




command, all I see is a black square and when I trying to download via downloadLink file's property, it even couldn't be opened.



So, here's my code



private static String initiateResumable(string base64, string fileType)
String endpoint ='https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';

map<String, Object> md = new map<String, Object>();

md.put('name', 'demofile');
md.put('parents', '[' + folderId + ']');

String metadata = JSON.serialize(md);

Integer size = Math.min(2000000, base64.length());

//return an HttpRequest object of POST method with
// Authorization token provided (JWT is used)
HttpRequest req = createRequest('post');

req.setEndpoint(endpoint);

req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setHeader('Content-Length', String.valueOf(metadata.length()));
req.setHeader('X-Upload-Content-Type', fileType);
req.setHeader('X-Upload-Content-Length', String.valueOf(size));

req.setBody(metadata);

HttpResponse res = (new Http()).send(req);

return res.getHeader('Location');


private static File sendFile(String base64, String fileType, String endpoint)
Integer size = Math.min(2000000, base64.length());
HttpRequest req = createRequest('put');
HttpResponse res;

req.setEndpoint(endpoint);

req.setHeader('Content-Type', fileType);

while(! String.isBlank(base64))
final string buffer = base64.left(size);
base64 = base64.removeStart(buffer);

req.setHeader('Content-Length', String.valueOf(buffer.length()));
req.setBody(buffer);

res = (new Http()).send(req);


return (File)JSON.deserialize(res.getBody(), File.class);



I believe that's because of base64 format, but I may be wrong



The same error occurs with the following code:



 HttpRequest req = createRequest('get');
req.setEndpoint('https://www.googleapis.com/upload/drive/v3/files?uploadType=media');

req.setHeader('Content-Type', fileType);
req.setHeader('Content-Length',String.valueOf(base64.length()));
req.setHeader('Content-Transfer-Encoding', 'base64');

req.setBody(base64);

HttpResponse res = (new Http()).send(req);

File file = (File) JSON.deserialize(res.getBody(), File.class);

return file;


It's Salesforce's Apex so no way for any frameworks, but the syntax is quite straightforward, I believe



10q in advance










share|improve this question
























  • Could you share any errors from your logs?

    – MαπμQμαπkγVπ.0
    Mar 7 at 8:51











  • Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

    – being of habits
    Mar 7 at 10:59












  • Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

    – being of habits
    Mar 7 at 11:08















0















I've created a folder and a file w/ permission to access a file for any Internet user. I used to think upload itself worked properly. Unfortunately, when I use




https://drive.google.com/uc?export=view&id=file.id




command, all I see is a black square and when I trying to download via downloadLink file's property, it even couldn't be opened.



So, here's my code



private static String initiateResumable(string base64, string fileType)
String endpoint ='https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';

map<String, Object> md = new map<String, Object>();

md.put('name', 'demofile');
md.put('parents', '[' + folderId + ']');

String metadata = JSON.serialize(md);

Integer size = Math.min(2000000, base64.length());

//return an HttpRequest object of POST method with
// Authorization token provided (JWT is used)
HttpRequest req = createRequest('post');

req.setEndpoint(endpoint);

req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setHeader('Content-Length', String.valueOf(metadata.length()));
req.setHeader('X-Upload-Content-Type', fileType);
req.setHeader('X-Upload-Content-Length', String.valueOf(size));

req.setBody(metadata);

HttpResponse res = (new Http()).send(req);

return res.getHeader('Location');


private static File sendFile(String base64, String fileType, String endpoint)
Integer size = Math.min(2000000, base64.length());
HttpRequest req = createRequest('put');
HttpResponse res;

req.setEndpoint(endpoint);

req.setHeader('Content-Type', fileType);

while(! String.isBlank(base64))
final string buffer = base64.left(size);
base64 = base64.removeStart(buffer);

req.setHeader('Content-Length', String.valueOf(buffer.length()));
req.setBody(buffer);

res = (new Http()).send(req);


return (File)JSON.deserialize(res.getBody(), File.class);



I believe that's because of base64 format, but I may be wrong



The same error occurs with the following code:



 HttpRequest req = createRequest('get');
req.setEndpoint('https://www.googleapis.com/upload/drive/v3/files?uploadType=media');

req.setHeader('Content-Type', fileType);
req.setHeader('Content-Length',String.valueOf(base64.length()));
req.setHeader('Content-Transfer-Encoding', 'base64');

req.setBody(base64);

HttpResponse res = (new Http()).send(req);

File file = (File) JSON.deserialize(res.getBody(), File.class);

return file;


It's Salesforce's Apex so no way for any frameworks, but the syntax is quite straightforward, I believe



10q in advance










share|improve this question
























  • Could you share any errors from your logs?

    – MαπμQμαπkγVπ.0
    Mar 7 at 8:51











  • Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

    – being of habits
    Mar 7 at 10:59












  • Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

    – being of habits
    Mar 7 at 11:08













0












0








0








I've created a folder and a file w/ permission to access a file for any Internet user. I used to think upload itself worked properly. Unfortunately, when I use




https://drive.google.com/uc?export=view&id=file.id




command, all I see is a black square and when I trying to download via downloadLink file's property, it even couldn't be opened.



So, here's my code



private static String initiateResumable(string base64, string fileType)
String endpoint ='https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';

map<String, Object> md = new map<String, Object>();

md.put('name', 'demofile');
md.put('parents', '[' + folderId + ']');

String metadata = JSON.serialize(md);

Integer size = Math.min(2000000, base64.length());

//return an HttpRequest object of POST method with
// Authorization token provided (JWT is used)
HttpRequest req = createRequest('post');

req.setEndpoint(endpoint);

req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setHeader('Content-Length', String.valueOf(metadata.length()));
req.setHeader('X-Upload-Content-Type', fileType);
req.setHeader('X-Upload-Content-Length', String.valueOf(size));

req.setBody(metadata);

HttpResponse res = (new Http()).send(req);

return res.getHeader('Location');


private static File sendFile(String base64, String fileType, String endpoint)
Integer size = Math.min(2000000, base64.length());
HttpRequest req = createRequest('put');
HttpResponse res;

req.setEndpoint(endpoint);

req.setHeader('Content-Type', fileType);

while(! String.isBlank(base64))
final string buffer = base64.left(size);
base64 = base64.removeStart(buffer);

req.setHeader('Content-Length', String.valueOf(buffer.length()));
req.setBody(buffer);

res = (new Http()).send(req);


return (File)JSON.deserialize(res.getBody(), File.class);



I believe that's because of base64 format, but I may be wrong



The same error occurs with the following code:



 HttpRequest req = createRequest('get');
req.setEndpoint('https://www.googleapis.com/upload/drive/v3/files?uploadType=media');

req.setHeader('Content-Type', fileType);
req.setHeader('Content-Length',String.valueOf(base64.length()));
req.setHeader('Content-Transfer-Encoding', 'base64');

req.setBody(base64);

HttpResponse res = (new Http()).send(req);

File file = (File) JSON.deserialize(res.getBody(), File.class);

return file;


It's Salesforce's Apex so no way for any frameworks, but the syntax is quite straightforward, I believe



10q in advance










share|improve this question
















I've created a folder and a file w/ permission to access a file for any Internet user. I used to think upload itself worked properly. Unfortunately, when I use




https://drive.google.com/uc?export=view&id=file.id




command, all I see is a black square and when I trying to download via downloadLink file's property, it even couldn't be opened.



So, here's my code



private static String initiateResumable(string base64, string fileType)
String endpoint ='https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';

map<String, Object> md = new map<String, Object>();

md.put('name', 'demofile');
md.put('parents', '[' + folderId + ']');

String metadata = JSON.serialize(md);

Integer size = Math.min(2000000, base64.length());

//return an HttpRequest object of POST method with
// Authorization token provided (JWT is used)
HttpRequest req = createRequest('post');

req.setEndpoint(endpoint);

req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setHeader('Content-Length', String.valueOf(metadata.length()));
req.setHeader('X-Upload-Content-Type', fileType);
req.setHeader('X-Upload-Content-Length', String.valueOf(size));

req.setBody(metadata);

HttpResponse res = (new Http()).send(req);

return res.getHeader('Location');


private static File sendFile(String base64, String fileType, String endpoint)
Integer size = Math.min(2000000, base64.length());
HttpRequest req = createRequest('put');
HttpResponse res;

req.setEndpoint(endpoint);

req.setHeader('Content-Type', fileType);

while(! String.isBlank(base64))
final string buffer = base64.left(size);
base64 = base64.removeStart(buffer);

req.setHeader('Content-Length', String.valueOf(buffer.length()));
req.setBody(buffer);

res = (new Http()).send(req);


return (File)JSON.deserialize(res.getBody(), File.class);



I believe that's because of base64 format, but I may be wrong



The same error occurs with the following code:



 HttpRequest req = createRequest('get');
req.setEndpoint('https://www.googleapis.com/upload/drive/v3/files?uploadType=media');

req.setHeader('Content-Type', fileType);
req.setHeader('Content-Length',String.valueOf(base64.length()));
req.setHeader('Content-Transfer-Encoding', 'base64');

req.setBody(base64);

HttpResponse res = (new Http()).send(req);

File file = (File) JSON.deserialize(res.getBody(), File.class);

return file;


It's Salesforce's Apex so no way for any frameworks, but the syntax is quite straightforward, I believe



10q in advance







google-api google-drive-sdk jwt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 16:33







being of habits

















asked Mar 6 at 16:24









being of habitsbeing of habits

394




394












  • Could you share any errors from your logs?

    – MαπμQμαπkγVπ.0
    Mar 7 at 8:51











  • Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

    – being of habits
    Mar 7 at 10:59












  • Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

    – being of habits
    Mar 7 at 11:08

















  • Could you share any errors from your logs?

    – MαπμQμαπkγVπ.0
    Mar 7 at 8:51











  • Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

    – being of habits
    Mar 7 at 10:59












  • Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

    – being of habits
    Mar 7 at 11:08
















Could you share any errors from your logs?

– MαπμQμαπkγVπ.0
Mar 7 at 8:51





Could you share any errors from your logs?

– MαπμQμαπkγVπ.0
Mar 7 at 8:51













Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

– being of habits
Mar 7 at 10:59






Unfortunately, there are no errors. Meant to say, that's told by google drive API dashboard. Moreover, the file created could be retrieved via '/files' and '/files/fileId' HTTP requests both

– being of habits
Mar 7 at 10:59














Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

– being of habits
Mar 7 at 11:08





Probably, it could be useful. That's an Id of a file creted: 19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0 It's downloadURL: doc-08-0o-docs.googleusercontent.com/docs/securesc/… And embedURL : drive.google.com/file/d/19FHn4Lxke_10exvF-C0Z4OjI3pnr77I0/…

– being of habits
Mar 7 at 11:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027804%2fgoogle-drive-api-for-service-account-error-while-creating-a-file-probably-due%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027804%2fgoogle-drive-api-for-service-account-error-while-creating-a-file-probably-due%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved