I need to transfer large volumes of files and so I decided to use the WeTransfer API.
First you need to register etc. and afterwards you will get an API-key. This I have.
With the API-key you can call the authorization for a 'token'.
In DOS this works fine but in my program I receive: "message":"Unauthorized"
I attach a short working demo but you will need to a valid API-key: this I can provide.
Demo:
Code: Select all
#include "hmg.ch"
#include "hbcurl.ch"
FUNCTION MAIN()
/***************/
PUBLIC cCurrentSRT := GetStartUpFolder()
PUBLIC cURL := ''
PUBLIC cApiUrl := "https://dev.wetransfer.com/"
PUBLIC cAPIKey := "my_key"
PUBLIC cPOSTdata
PUBLIC h, curlHandle, i, ahResp
/*
DOS ok
C:\curl-7.53.1-win32-mingw\bin>
curl -i -X POST "https://dev.wetransfer.com/v2/authorize" -H "Content-Type: application/json" -H "x-api-key: my_key"
RESPONSE:
{
"success": true,
"token": "some_token"
}
OR:
{
"message":"Unauthorized"
}
*/
SET PRINTER TO C:\TEST\TEST_WT.TXT
SET PRINTER ON
SET CONSOLE OFF
Get_WETR_Authorization()
SET PRINTER TO
SET PRINTER OFF
SET CONSOLE ON
RETURN NIL
FUNCTION Get_WETR_Authorization()
/**********************************************************/
LOCAL cURL, i, h
? 'Get_WETR_Authorization: Authorization for WETRANSFER'
cURL := cApiUrl + 'v2/authorize'
curlHandle := curl_easy_init()
IF EMPTY(curlHandle)
? "Error while init cURL lib."
MsgStop("Error while init cURL lib.")
RETURN
ENDIF
// see https://developers.wetransfer.com/documentation
// Authorization
h := { => }
h [ "x-api-key" ] := cAPIKey
h [ "Content-Type" ] := "application/json"
cPOSTdata := hb_jsonEncode( h , .T. )
cResp := Send_WeTransfer( cUrl, cPOSTdata, curlHandle )
? 'cURL', cURL
? 'cPOSTdata', cPOSTdata
? 'cResp', cResp
? 'cAPIKey', cAPIKey
hb_jsonDecode( cResp , @ahResp)
? 'ahResp' , ahResp
? 'cResp ', cResp
? 'LEN( ahResp )', LEN( ahResp )
RETURN
FUNCTION Send_WeTransfer (cUrl, cPOSTdata, curlHandle)
/****************************************************/
Local curlErr, cReturn
curl_easy_reset( curlHandle )
curl_easy_setopt( curlHandle, HB_CURLOPT_URL, cUrl )
curl_easy_setopt( curlHandle, HB_CURLOPT_FOLLOWLOCATION, .T. )
curl_easy_setopt( curlHandle, HB_CURLOPT_SSL_VERIFYPEER, .F. )
curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( curlHandle, HB_CURLOPT_USERAGENT, "WeTransfer-Curl/1.0")
curl_easy_setopt( curlHandle, HB_CURLOPT_POST, .T. )
curl_easy_setopt( curlHandle, HB_CURLOPT_POSTFIELDSIZE, LEN( cPOSTdata ) )
curl_easy_setopt( curlHandle, HB_CURLOPT_POSTFIELDS, cPOSTdata )
curlErr := curl_easy_perform( curlHandle ) /* Do everything */
IF !EMPTY( curlErr ) /* Report any errors */
cReturn := "!ERROR!" + CRLF + curl_easy_strerror(curlErr)
ELSE
cReturn := curl_easy_dl_buff_get( curlHandle )
ENDIF
RETURN cReturn
Thanks, Serge