Hello everyone. I’m going crazy with DropBox Api Http
Never used an Api Call and don’t know how to manage it.
What should I Do :
- Get access to my Public Folder
- Find Sharable Link for images
My Question
How should be the script to “send the request”? (can’t figure out …) (need a full script)
How should be the “response” and how I should deserialize it (JsonUtily?)
Thank you a lot.
For now I only figured out that I should use WebRequest (but I don’t know how).
Already configured DropBox App and I have the Token.
From here and on I cannot understand what I should do…
I dont know how dropbox works, if they only upload changes to files or they upload the full file everytime (i imagine the first one) but you need a php script on your server (app?) to handle the upload of files, here is a small example of a zip upload (I have edited it because it is code from an old project with extra stuff you dont need so maybe I deleted something but is pretty much this)
WWWForm formData = new WWWForm ();
byte[] formBytes;
formData.AddField("action", "zipupload");
if (File.Exists("path/to/the/files/example.zip")) //HERE YOU FIND THE FILE TO UPLOAD
{
formBytes = File.ReadAllBytes("path/to/the/files/example.zip");
formData.AddBinaryData("form", formBytes, "Form.zip");
}
string url = "http://URLtoYourSite/upload.php"; //CHANGE TO YOUR URL
using (UnityWebRequest www = UnityWebRequest.Post (url, formData)) {
string authorization = Authenticate (stUsername, stPassword); //only use this if you need user password
byte[] boundary = UnityWebRequest.GenerateBoundary ();
www.SetRequestHeader ("Authorization", authorization);
yield return www.SendWebRequest ();
if (www.isNetworkError || www.isHttpError) {
Debug.LogError (www.error);
} else {
Debug.Log ("Files uploaded.");
StringBuilder sb = new StringBuilder ();
foreach (KeyValuePair<string, string> dict in www.GetResponseHeaders ()) {
sb.Append (dict.Key).Append (": [").Append (dict.Value).Append ("]
");
}
Debug.Log ("Response headers: " + sb.ToString ());
Debug.Log ("Response text: " + www.downloadHandler.text);
}
}
`then you need the php to move the files to your location in your server
AFTER ABOUT 20HR OF STUDY
I’ve managed to have a working Script…Really I fell like I was under torture :-). This script maybe is not “Super Clean” (I’ve a lot of missing info about true coding). It took the “API URL”, pass BODY PARAMETERS, pass Authorization Token. I was unable to use the WebRequest.Post because of Json Decode Warning. In the way below insted it work! Thank you for Help @xxmariofer. (Now I’m stucked in JsonDeSerialize but maybe i will use some dirty String Search, only need to find Shared_Link in Response)
public string Url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings";
public string Token = "MySuperSecretToken";
public string Path = "/Shared Folder";
// STRUCT DEI PARAMETRI DEL BODY
public struct MyStruct
{
public string path;
}
// GENERO STRUCT
public MyStruct DataFormBody;
IEnumerator Post_Easy()
{
// Assegno la Path
DataFormBody.path = Path;
// Converto in JSon
string JSonBody = JsonUtility.ToJson(DataFormBody);
// Passo il Parametro POST a UnityWebRequest
var request = new UnityWebRequest(Url, "POST");
// Codifico il Json in Byte Utf8
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(JSonBody);
// Assegno il Body in Parametro Upload
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
// Assegno il Download del Response
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
// Imposto l'Header di Autorizzazione
request.SetRequestHeader ("Authorization", "Bearer " + Token);
// Imposto la tipologia di comunicazione
request.SetRequestHeader ("Content-Type", "application/json");
// Invio la richiesta alla API DropBox
yield return request.Send();
// Verifica se ci sono errori di connesione
if (request.isNetworkError || request.isHttpError)
{
// Riporta l'errore di connesione
Debug.Log(request.error);
// Riporta ulteriori informazioni riguardo l'errore
string message = request.downloadHandler.text;
Debug.Log("response: " + message);
}
else
{
// Scarica la risposta
string message = request.downloadHandler.text;
// Debug Status Code
Debug.Log("Status Code: " + request.responseCode);
// Debug Risposta
Debug.Log("response: " + message);
// Scrivo la risposta in un file
File.WriteAllText("DropBox Response.txt",message);
}
}