Upload Video with the Imgur RapidAPI - imgur

So I know this question has been asked here before but not specifically with RapidAPI. Anyways, the RapidAPI page forwards the documentation to https://api.imgur.com/
which further forwards to https://apidocs.imgur.com/
where finally we can find the api endpoint https://api.imgur.com/3/upload
now this endpoint is not compatible with the rapid api because there is simply no /3/upload endpoint on rapidapi. although it is written in the documentation that /3/upload is the same as /3/image which would make https://api.imgur.com/3/upload into https://imgur-apiv3.p.rapidapi.com/3/image. Problem here is that when i make a request to this endpoint with a video instead of an image in the payload i get this response json:
{'data': {'error': 'Could not process upload!', 'request': '/3/image', 'method': 'POST'}, 'success': False, 'status': 500}
Which basically tells me that it cannot parse the file format because when uploading any type of image it works. So what's going on?

This works for me:
import requests
url = "https://api.imgur.com/3/upload"
payload = {'album': 'ALBUMID',
'type': 'file',
'disable_audio': '0'}
files = [
('video', open('/path/to/Video.mp4','rb'))
]
headers = {
'Authorization': 'Bearer BEARERTOKENHERE'
}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))

As far as I can understand their API /3/upload is for video & image but requires a token to use.
/3/image is for images only and should be used for uploading anonymous images with no user authentication.
But I am not 100% sure about that since I was not able to get a token to upload to /upload myself. (since I wanted to upload to my account in a 2 legged OAuth which looks like is not supported?)
So what you would need to do is to call /auth to get your token, exchange this token for a BEARER TOKEN to use the /upload path.
Hope this explains it a bit.

Related

Python Requests to Forge API - 401 Response "Authorization failed"

def get_token():
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'client_id': 'XXX', 'client_secret': 'XXX', 'grant_type': 'client_credentials', 'scope': 'data:read'}
response = requests.post('https://developer.api.autodesk.com/authentication/v1/authenticate', headers=headers, data=data)
return response.json()['access_token']
print('Bearer ' + get_token())
response_form_templates = requests.get('https://developer.api.autodesk.com/construction/forms/v1/projects/:projectId/form-templates', headers={'Authorization': 'Bearer ' + get_token()})
print(response_form_templates.json())
print(response_form_templates)
I'm trying to use python requests to simply get back the form-templates and all I'm getting back is a 401 response and a message saying "Authorization failed". The few print statements in there are from my trying to debug what's going wrong, but with my client_id and client_secret in there, it gives me those errors.
Any idea as to what could be wrong would be really helpful, thanks.
Btw. we don't have an official Forge SDK for Python yet but there's a simple, unofficial one here: https://github.com/petrbroz/forge-sdk-python that you could perhaps use as a reference when building the HTTP requests yourself. For example, here's how the SDK retrieves the 2-legged access token: https://github.com/petrbroz/forge-sdk-python/blob/develop/src/autodesk_forge_sdk/auth.py#L147-L178.
You are using a 2-legged token instead of a 3-legged token. As you can see from the image below, you require a 3-legged token when you want to retrieve form templates.
Use this link and see how you can get a 3-legged token.

File streamed from node and made into blob on javascript client is much larger and corrupted

I am working with the Clio API, a third party API that has some cloud based document storage. I would like our users to download video/audio documents from their API but, due to it's design, I have to use my node.js server as a proxy to get around CORS restrictions.
The flow is:
Users make a request to download document to my server
My server makes a request to the API to download the document
My server streams response to client
Because the documents may be large (1GB+), I am attempting to stream the files fetched by my server to the client.
The problem is that the files received on the client are corrupted and larger than the actual files. Here is how I am currently making the requests (note: we typically use axios but I am not sure how to do what I do below with that package, so for now I am using the request package for proof of concept):
For an MP4 file that is about 220kb in size:
request({
url: `https://app.clio.com/api/v4/documents/${documentId}/download.json`,
headers: { Authorization: `Bearer ${accessToken}` },
}).pipe(res);
Then on the client we receive the response like:
response.blob()
The confounding thing is that the resulting blob is as such:
Blob {size: 407852, type: "application/mp4"}
The size is almost double the actual size of the file and I can not understand why. Furthermore, downloading and playing the mp4 from here is impossible because the mp4 file is corrupted and can't be played. This is especially strange because if I decide to pipe the server request to a file on my server like so:
const filepath = path.resolve(__dirname, "test.mp4");
const writer = fs.createWriteStream(filepath);
request({
url: `https://app.clio.com/api/v4/documents/${documentId}/download.json`,
headers: { Authorization: `Bearer ${accessToken}` },
}).pipe(writer);
The resulting "test.mp4" has the correct size and plays okay.
The issue seems to be with streaming the file to the client specifically. When I attempt to view the raw data that arrives on the client (via response.text()), a sample of what I see is the following:
\u0000\u0000\u0000\u001cftypmp42\u0000\u0000\u0000\u0001mp41mp42isom\u0000\u0000\u0000\u0001mdat\u0000\u0000\u0000\u0000\u0000\u0003UJ!*Te&(�B��4�p*hUī\u0012��Т�\u0012ɾ���0�P}�C$��}<��Gc�^�#~��ʠ6�^��m���<E���Y\u0013\t(Ѳ��x�\u0015/3c�(�����\u000b�4O\u0006�{$�d_J�\\j���^�ԑ\u0017~�&��iǠN�QV��\t\u0006%�B��05\u0012�kW�l\u0006hfZ�q7\u0000\u0006�v%��G���/2*\u000e\u0001-�^^6i�|
This appears to be UTF8 encoded data with a number of - I'm guessing - replacement characters. I think this may be what is inflating the size of the blob and corrupting the mp4 file.
What could be happening?
I was facing the same problem.I added below line to my axios configuration (client side). It solved my issue.
responseType: 'arraybuffer'

Getting gibberish when trying to download file from sharepoint using microsoft graph api

I'm trying to download some excel file from our sharepoint. I'm following instructions from this resource: https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
when I try to call the endpoint from Postman, I get some gibberish and a pretty long one. the tutorial says I'm supposed to get a 302 response but I get a 200 response.
I also have all the permission granted for read write for Site and Files.
from postman, I translate the code to python. here's my code. I get the same result on my python script:
url = "https://graph.microsoft.com/v1.0/drives/<our_drive_id>/items/<target_file_id>/content"
payload={}
headers = {
'Content-Type': 'application/json',
'SdkVersion': 'postman-graph/v1.0',
'Authorization': f'Bearer {token}' # retrieved from an endpoint called prior
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response)
here is the response that I get:
I'm not really sure since I don't get an actionable error. Also the response that I get is different from the expected response stated in the tutorial.
The documentation says that the request returns a 302 Found response redirecting to a pre-authenticated download URL for the file.
Postman follows the Location header in the response and makes a redirect to URL and downloads the content.
If you disable Automatically follow redirects in Postman settings then the response will be 302 and Location header will contain a download link.

How to upload video from the API imgur

I'm using imgur API and I have several question, what is the size limit for uploading a .gif ? I read everywhere the size limit of picture is 10Mb and gif are 20Mb but when I try to upload a gif of 11Mb with the API it tells me it's too heavy.
Also Im trying to upload a video (.mp4 , 6,9Mb) and I use the following request with postman
https://api.imgur.com/3/image?client_id={myclientid}
and in the body
video={mavideo.mp4}
but it return me a success with no information
{
"data": {
"errorCode": null,
"ticket": "90c70cdc"
},
"success": true,
"status": 200
}
I have followed the api documentation to make this request
Thank you.
EDIT:
Ok it work if I'm using
https://api.imgur.com/3/upload in postman but when I try to use it with ajax like this
if (fileExtension == "mp4" || fileExtension == "webm"){
form.append('video', file);
} else {
form.append('img', file);
}
$.ajax({
url: 'https://api.imgur.com/3/upload',
headers: {
Authorization: "Client-ID " + ClientId },
type: 'POST',
data: form,
cache: false,
contentType: false,
processData: false
})
I have got the message Access to XMLHttpRequest at 'https://api.imgur.com/3/upload' from origin 'http://localhost:3010' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
What is wrong ?
Not really an answer, but we both seem to be trying to get this working at the same time, so i will share what i found out.
As you seem to have found out also, there are 2 endpoints for uploading, /image and /upload.
When uploading video, /image returns a ticket id. This is probably meant to get the state of the queued processing. There is however no endpoint available where you can send the ticket id and get the state back.
Second endpont, /upload has however another bunch of issues.
While it does return a more useable object with image id and url, it seems to completely ignore the authorization header. Any image that i managed to upload this way has null for the uploaded user, and i don't see it in my account. (using oauth2 and sending Authorization: Bearer <token>) Even if i omit the header completely, it still works even though Authorization is mandatory for uploading.
Another weird thing is, if i don't send the auth header BUT add anything to the query params (so ?some=thing or whatever) it suddenly wakes up and starts requiring the authorization header. When i send it again at this point however, we are back at the beginning and even the /upload endpoint starts sending ticket id and not a reasonable response.
This is a CORS issue and needs to be fixed by the imgur devs. It's only an issue when doing client-side JavaScript as curl works just fine:
echo 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' | base64 --decode | curl --location --request POST 'https://api.imgur.com/3/upload' --header "Authorization: Client-ID $CLIENT_ID" -F 'type=file' -F 'disable_audio=0' -F 'image=#-'

shopify - nodejs - get permanent token fails

I have written an application that talks with the shopify API. I manage to get the temporary code from shopify and redirect back to my app where I store the code to later exchange for the permanent token.
According to the docs all I need to do is then send a POST request to https://{shop}.myshopify.com/admin/oauth/access_token with the client_id, client_secret and code is the body of the request.
I am using the request module to send the request and have it set up to send the request as such:
var options = {
method: POST,
url: https://my-develop-shop.myshopify.com/admin/oauth/access_token,
json: true
};
var _body = {
"client_id": config.get('SHOP_ID'),
"client_secret": config.get('SHOP_SECRET'),
"code": tempCode
}
_body = JSON.stringify(_body);
options.body = _body;
request(options, callback);
However when I send the request it always returns with : error_description: 'Could not find Shopify API application with api_key ' }
The app is installed successfully on the client's shop, so why would this error be returned?
Is there a special header that shopify expects? The docs are so vague.
Why does it not authenticate?
Well I cheated and used the shopify-node-api package. There I just use the exchange_temporary_token method. This api also handles throttling so it's a decent investment in the time you might spend incorporating it.

Resources