Unable to get content of file using google drive api - google-api

I am working with a react application where you can access google drive files using react-google-picker and I am using get api of google apis where you can pass "alt=media" as a URL parameter to get the file content. It's working as expected for text files but for PDF/Doc files, it returns metadata instead of actual content of file.
Is there any other way to get the content using google api's ??
this is the code for getting content of the file.
axios.get(`https://www.googleapis.com/drive/v2/files/${data.docs[0].id}?key=${developerKey}&alt=media`, {
headers: {
Authorization: Bearer ${this.state.token}
}
})

Related

How can I save an image via API in Laravel Server from React Native

I am trying to save some info and an image sent through api from React Native app. This is the request that I get if I log it.
[2022-08-05 10:11:09] local.ERROR: array ('{
"data":{
"_parts":' => array ('[
"image", {
"modificationDate":"1659694264000","size":2883040,"mime":"image/jpeg","height":4160,"width":3120,"path":"file:///storage/emulated/0/Android/data/com.wheeloffortune/files/Pictures/image-6ab1ad33-2580-416b-be7d-09a0662739218182247041115654861.jpg"
}' => NULL,
),)
When I tested api from postman I get image as a file and have saved it easily without a sweat. But from the app the developer sends in the format I pasted above. Can I somehow save image from the above request.

How to Create/Load and Download a File throught Google Drive API?

I'm trying to create a file in Google Drive through Google Drive API.I managed to create the file, but I couldn't load the attachment in the file.
To create the file I Have used this Endpoint
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media
with the Content-type : application/pdf header
And this body (but it was not taken from google => the file generated don't have a title and the description):
{
"title": "prova.pdf",
"mimeType": "application/pdf",
"description": "Stuff about the file"
}
Then I tried to load the attachment in this file (using base64 encoding)through a call to this endpoint
PATCH https://www.googleapis.com/upload/drive/v3/files/{FILEID}
with thesetwo headers:
Content-type: application/pdf
Content-Transfer-Encoding: base64
And in the body (raw - text) I put the base64 encoded file.
The call doesn't return an error but now if I open the file on the Google Drive I see the base64 string in place of the pdf file.
Then I tried downloading the file. I tried with
GET https://www.googleapis.com/drive/v2/files/{fileId}
but this call retrieves a 'webContentLink' so I have to open this link in the Browser, authenticate with my account and then the download starts.
It's possible to retrieve the document as a base64 string directly from the call?

Error "SignatureDoesNotMatch". Google Cloud Storage Bucket PUT

I'm loosing my mind.
I'm using Shrine (https://github.com/janko-m/shrine) with Google Cloud Storage (https://github.com/renchap/shrine-google_cloud_storage), but when I start the PUT call I get this:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>
PUT
image/jpeg
1518399402
/mybucket.appspot.com/7d5e4aad1e3a737fb8d2c59571fdb980.jpg
</StringToSign>
</Error>
I followed this info (http://shrinerb.com/rdoc/classes/Shrine/Plugins/PresignEndpoint.html) for presign_endpoint, but still nothing:
class FileUploader < Shrine
plugin :presign_endpoint, presign_options: -> (request) do
filename = request.params["filename"]
extension = File.extname(filename)
content_type = Rack::Mime.mime_type(extension)
{
content_type: content_type
}
end
end
I tried with and without this (restarting the Rails server everytime).
Where am I wrong?
I also tried with Postman with a PUT to that URL and withtout any content-type. But still nothing.
I read here: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1976 and here: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1695
How can I try without Rails?
Is there a REPL (or similar) to try with my credentials and with a file?
You have several options for just uploading a file to Google Cloud Storage as you can see in the official docs here.
For example if you want to use Ruby Client Library, you can use this code:
# project_id = "Your Google Cloud project ID"
# your-bucket-name = "Your Google Cloud Storage bucket name"
# local_file_path = "Path to local file to upload"
# storage_file_path = "Path to store the file in Google Cloud Storage"
require "google/cloud/storage"
storage = Google::Cloud::Storage.new(project: "your-project_id")
bucket = storage.bucket "your-bucket-name"
file = bucket.create_file "local_file_path", "storage_file_path"
puts "Uploaded #{file.name}"
You will need to:
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.
as you can see in the Cloud Storage Client Libraries docs here.
However, it looks like the github repo you are using makes use of signed URLs. If you need to create a signed URL you have the instructions here. It exists already the signed_urls method for Ruby in the official repo.
Anyway, the error you were getting is because there was something wrong being made with the signed urls. And precisely there was a commit to the repo you were referencing (https://github.com/renchap/shrine-google_cloud_storage) 7 days ago with the name Fix presigned uploads. It looks like this commit should fix the "PUT" upload (before a "GET" was being signed, therefore it wouldn't work). Still I didn't try it so I don't know if it is actually working.

How to get Uploaded Url of a Video File in android using Parse REST API

I am using Parse Mobile Backend for my android app,but whenever I want to upload a large video more than the default 10mb limit for a ParseFileby getting the bytes from it into a ParseFile I keep running into the dreaded OutOfMemory Exception .So,I want to use the Parse REST API since I can easily use the setChunkedStreamingMode(1024) in HttpUrlConnectionto send the bytes in a chunked manner.The trouble is,how do I get the uploaded url of the file uploaded.Thanks for your help in advance.
i think you can use getUrl() method
ParseFile file = new ParseFile(byte[] data);
file.getUrl();
ParseFile object has getUrl() method to getting url of file. this method return URL in string format.

how to read text file from django server using ajax?

I wanna know how to read text file from django server using ajax? or something else.
I searched about using XMLHttpRequest and It worked when I tryed to get django template html file. but I couldn't get other text file outside of templates directory. how can I send content of requested file in veiw.py ??
I think I cant use render_to_response()..
I need your help:(
Simething like this in your url:
url(r'^static/mymedia/' , 'myproject.views.get_file'),
Something like this in your views:
def get_file(request):
current_directory='/home/arpit/myproject/'
f=open(current_directory+request.path)
return HttpResponse(f.read(),mimetype='text/plain')

Resources