public bool Login(string email, string password) { Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/token"); http.Parameters.Add(new HttpParameter { Name = "oauth_consumer_key", Value = _apiKey }); http.Parameters.Add(new HttpParameter { Name = "email", Value = email }); http.Parameters.Add(new HttpParameter { Name = "password", Value = password }); //add oauth OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, "", ""); oAuth.Authenticate(http); try { http.Get(); if (http.Response.StatusCode == HttpStatusCode.Unauthorized) { //Wrong Username/Password throw new DropException("Incorrect Username/Password."); } BytesRecieved += http.Response.Content.Length; JsonDeserializer deserializer = new JsonDeserializer(); UserLogin = deserializer.Deserialize<UserLogin>(http.Response.Content); if (!string.IsNullOrEmpty(UserLogin.Error)) { //Some sort of dropbox error throw new DropException(UserLogin.Error); } LoggedIn = true; return LoggedIn; } catch (DropException dx) { throw dx; } catch (Exception ex) { return false; } }
public AccountInfo GetAccountInfo() { if (_accountInfo != null) { var lastDiff = DateTime.Now - _lastAccountInfo; //May need Tweeking if (lastDiff.TotalSeconds < 100) return _accountInfo; } Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/account/info"); OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret); oAuth.Authenticate(http); try { http.Get(); BytesRecieved += http.Response.Content.Length; JsonDeserializer deserializer = new JsonDeserializer(); _accountInfo = deserializer.Deserialize<AccountInfo>(http.Response.Content); return _accountInfo; } catch (Exception ex) { return null; } }
public MetaData CreateFolder(MetaData parent, string folderName) { Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/fileops/create_folder"); OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret); http.Parameters.Add(new HttpParameter { Name = "path", Value = string.Format("{0}/{1}", parent.Path, folderName) }); http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" }); oAuth.Authenticate(http); try { http.Get(); BytesRecieved += http.Response.Content.Length; JsonDeserializer deserializer = new JsonDeserializer(); var returnMeta = deserializer.Deserialize<MetaData>(http.Response.Content); if (string.IsNullOrEmpty(returnMeta.error)) { if (returnMeta.Contents == null) returnMeta.Contents = new List<MetaData>(); return returnMeta; } else { return null; } } catch (Exception ex) { return null; } }
public bool MoveFile(MetaData fromFile, MetaData toDir) { Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/fileops/move"); OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret); http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" }); http.Parameters.Add(new HttpParameter { Name = "from_path", Value = fromFile.Path }); http.Parameters.Add(new HttpParameter { Name = "to_path", Value = string.Format("{0}/{1}", toDir.Path, fromFile.Name) }); oAuth.Authenticate(http); try { http.Get(); BytesRecieved += http.Response.Content.Length; return http.Response.StatusCode == HttpStatusCode.OK; } catch (Exception ex) { return false; } }
public bool Delete(MetaData remoteFile) { Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/fileops/delete"); OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret); http.Parameters.Add(new HttpParameter { Name = "path", Value = remoteFile.Path }); http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" }); oAuth.Authenticate(http); try { http.Get(); BytesRecieved += http.Response.Content.Length; return http.Response.StatusCode == HttpStatusCode.OK; } catch (Exception ex) { return false; } }
public MetaData GetItems(MetaData remoteDir) { Http http = new Http("get"); http.Url = new Uri("https://api.dropbox.com/0/metadata/dropbox" + remoteDir.Path); OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret); oAuth.Authenticate(http); try { http.Get(); BytesRecieved += http.Response.Content.Length; JsonDeserializer deserializer = new JsonDeserializer(); var returnMeta = deserializer.Deserialize<MetaData>(http.Response.Content); if (string.IsNullOrEmpty(returnMeta.error)) { if (returnMeta.Contents == null) returnMeta.Contents = new List<MetaData>(); return returnMeta; } else { return null; } } catch (Exception ex) { return null; } }