- Home /
DropBox Https Api Request : What should I do?
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 : 1. Get access to my Public Folder 2. 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...
Answer by xxmariofer · Dec 14, 2020 at 09:27 AM
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 (": \t[").Append (dict.Value).Append ("]\n");
}
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
Hi, first of all thank you for your contribute. As far as I understood I should only send some call, not php.
This is what DropBox Report Request and response formats In general, the Dropbox API uses HTTP POST requests with JSON arguments and JSON responses. Request authentication is via OAuth 2.0 using the Authorization request header or authorization URL parameter. The .tag field in an object identifies the subtype of a struct or selected member of a union. When specifying a Void member of a union, you may supply just the member string in place of the entire tagged union object. For example, when supplying a Write$$anonymous$$ode, you can supply just "mode": "add" instead of "mode": {".tag": "add"}}. This shorthand is not allowed for non-Void members. For example, the following is not allowed for a Write$$anonymous$$ode, as update is not a Void member: "mode": "update".
Oh! i though you were trying to build your own Dropbox app. Without knowing at all how the dropbox api works here is a sample code that does what you asked, you just need to use JsonUtility to parse the send and inco$$anonymous$$g data (you need to create a class as a base for the parse)
WWWForm form = new WWWForm();
form.AddField("json", data);
using (UnityWebRequest www = UnityWebRequest.Post("http://URL", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
string message = www.downloadHandler.text;
Debug.Log("response: " + message);
}
}
Thank you a lot! For now I have no idea what it does... but I will try to figure out how to use it. ( I'm just a begginer...some advanced form of code I'm unable to unterstand clearly...)
As far I can understand http://URL should pass the "access" to DropBox Api...than I "recall" it again with others Api codes.
Ok... it should work. Thank you a lot for your time. Really appreciated.
Answer by altropetrolio · Dec 22, 2020 at 06:04 AM
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);
}
}
Hello, I am working on a similar project and am having a problem with getting the token. Could you tell me how you generated the secret token for the code .
Your answer
Follow this Question
Related Questions
How to avoid reestablishing an HTTPS request, use Connection: Keep-Alive or reuse WWW object? 2 Answers
What is a good cross-platform alternative to WWW? 0 Answers
Image upload API works with postman but not working with Unity. -1 Answers
How to use UnityWebRequest to upload a File via Invision REST API 0 Answers