public async Task <ResponseContainer> MetadataTopFolder(SynchroManager _manager, Boolean _reentrant) { String _request = API_METADATA + "/sandbox?access_token=" + Token; HttpResponseMessage _response = await SendRequest(_request); if (_response != null) { if (_response.StatusCode == HttpStatusCode.OK) { return(await Serialization.DeserializeHttpToJson <ResponseContainer>(_response.Content) as ResponseContainer); } else if ((_response.StatusCode == HttpStatusCode.Unauthorized) || (_response.StatusCode == HttpStatusCode.NotFound)) { if (_reentrant) { await Login(); } //return await MetadataTopFolder(_manager, false); return(null); } else { ManageError(_response.StatusCode); } } return(null); }
public async Task <ResponseContainer> CreateFolder(String _path) { String _request = API_FILEOPS_CREATE + "?access_token=" + Token + "&root=sandbox&" + "path=" + (_path); HttpResponseMessage _response = await SendRequest(_request); return(await Serialization.DeserializeHttpToJson <ResponseContainer>(_response.Content) as ResponseContainer); }
public async Task Login_callback(String _code) { String _dropboxURL = "https://api.dropbox.com/1/oauth2/token?code=" + Uri.EscapeDataString(_code) + "&grant_type=authorization_code"; HttpResponseMessage _response = null; HttpClient httpClient = new HttpClient(); HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("client_id", ClientId), new KeyValuePair <string, string>("client_secret", Secret) }); try { _response = await httpClient.PostAsync(new Uri(_dropboxURL), content); } catch (HttpRequestException e) { Debug.WriteLine(e.Message); } if ((_response != null) && (_response.StatusCode == HttpStatusCode.OK)) { ResponseToken _t = await Serialization.DeserializeHttpToJson <ResponseToken>(_response.Content) as ResponseToken; Token = _t.access_token; LocalSettings.SaveStorageValue <String>(LOCAL_OAUTH_TOKEN, Token); //Get UserName String _respJson = await httpClient.GetStringAsync(new Uri("https://api.dropbox.com/1/account/info?access_token=" + Token)); if (_response != null) { var value = JsonValue.Parse(_respJson).GetObject(); UserName = value.GetNamedString("display_name"); LocalSettings.SaveStorageValue <String>(LOCAL_USER_NAME, UserName); //end of auth process LoggedIn = WebAccountState.Connected; // force synchronization if (_handle != null) { _handle.SetSynchroStatus(SynchroManager.Status.InProgress); await Synchronize(_handle); } } } return; }
private async Task UpdateToken() { // request token from auth code String GooglePlusURL = "https://accounts.google.com/o/oauth2/token"; String _refreshToken = (String)LocalSettings.LoadStorageValue <String>(LOCAL_OAUTH_TOKEN_SECRET); HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("client_id", Id), new KeyValuePair <string, string>("refresh_token", _refreshToken), new KeyValuePair <string, string>("client_secret", Secret), new KeyValuePair <string, string>("grant_type", "refresh_token") }); HttpResponseMessage _response = null; try { _response = await(new HttpClient()).PostAsync(new Uri(GooglePlusURL), content); } catch (HttpRequestException e) { Debug.WriteLine(e.Message); } if ((_response != null) && (_response.StatusCode == HttpStatusCode.OK)) { LoggedIn = WebAccountState.Connected; ResponseToken _responseToken = await Serialization.DeserializeHttpToJson <ResponseToken>(_response.Content) as ResponseToken; Token = _responseToken.access_token; ExpiryDate = DateTime.Now + new TimeSpan(0, 0, _responseToken.expires_in); LocalSettings.SaveStorageValue <String>(LOCAL_OAUTH_TOKEN, Token); LocalSettings.SaveStorageValue <long>(LOCAL_OAUTH_EXPIRY_DATE, ExpiryDate.Ticks); } }
public async override Task Login() { try { // try refresh token first RefreshToken = LocalSettings.LoadStorageValue <String>(LOCAL_OAUTH_TOKEN_REFRESH); if ((RefreshToken != null) && (RefreshToken != "")) { await UpdateToken(); } // refresh succeedeed, stop and return if (LoggedIn == WebAccountState.Connected) { return; } String GooglePlusURL = "https://accounts.google.com/o/oauth2/auth?" + "client_id=" + Uri.EscapeDataString(ClientId) + "&redirect_uri=" + Uri.EscapeDataString(CallbackUrl) + "&response_type=code" + "&scope=" + Uri.EscapeDataString("http://picasaweb.google.com/data"); Uri StartUri = new Uri(GooglePlusURL); // When using the desktop flow, the success code is displayed in the html title of this end uri Uri EndUri = new Uri("https://accounts.google.com/o/oauth2/approval?"); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.UseTitle, StartUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { String webAuthResultResponseData = WebAuthenticationResult.ResponseData.ToString(); // Get auth code string responseData = webAuthResultResponseData.Substring(webAuthResultResponseData.IndexOf("code")); String[] keyValPairs = responseData.Split('&'); string authorization_code = null; for (int i = 0; i < keyValPairs.Length; i++) { String[] splits = keyValPairs[i].Split('='); switch (splits[0]) { case "code": authorization_code = splits[1]; break; } } // test if first step failed for some reason if (authorization_code == null) { return; } // request token from auth code GooglePlusURL = "https://accounts.google.com/o/oauth2/token"; HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("code", authorization_code), new KeyValuePair <string, string>("client_id", ClientId), new KeyValuePair <string, string>("client_secret", Secret), new KeyValuePair <string, string>("redirect_uri", CallbackUrl), new KeyValuePair <string, string>("grant_type", "authorization_code") }); HttpResponseMessage _response = null; try { _response = await(new HttpClient()).PostAsync(new Uri(GooglePlusURL), content); } catch (HttpRequestException e) { Debug.WriteLine(e.Message); } if ((_response != null) && (_response.StatusCode == HttpStatusCode.OK)) { LoggedIn = WebAccountState.Connected; ResponseToken _responseToken = await Serialization.DeserializeHttpToJson <ResponseToken>(_response.Content) as ResponseToken; Token = _responseToken.access_token; RefreshToken = _responseToken.refresh_token; ExpiryDate = DateTime.Now + new TimeSpan(0, 0, _responseToken.expires_in); LocalSettings.SaveStorageValue <String>(LOCAL_OAUTH_TOKEN, Token); LocalSettings.SaveStorageValue <String>(LOCAL_OAUTH_TOKEN_REFRESH, RefreshToken); LocalSettings.SaveStorageValue <long>(LOCAL_OAUTH_EXPIRY_DATE, ExpiryDate.Ticks); GetGooglePlusUserNameAsync(Token); } } } catch (Exception Error) { Debug.WriteLine(Error.ToString()); } return; }
private async Task <Boolean> synchronizeAlbum(Album _album, String _pathTrip, TripSummary _summary) { String _pathAlbum = "/" + _pathTrip + "/" + _album.PathAlbum; ResponseContainer _folderDesc = null; ProgressUpdate(Res.GetString("SynchroAlbum") + " \"" + _album.Summary.Name + "\"", 0); //list top-folder String _request = API_METADATA + "/sandbox/" + _pathAlbum + "?access_token=" + Token + "&hash=" + _album.Hash; HttpResponseMessage _response = await SendRequest(_request); if (_response != null) { if (_response.StatusCode == HttpStatusCode.OK) { _folderDesc = await Serialization.DeserializeHttpToJson <ResponseContainer>(_response.Content) as ResponseContainer; } else if (_response.StatusCode == HttpStatusCode.NotFound) { _folderDesc = await CreateFolder(_pathTrip + "/" + _album.PathAlbum); } } if (_folderDesc != null) { Boolean _status = true; if ((_folderDesc.contents != null) && (_folderDesc.contents.Count() != 0)) { //check if all dropbox pictures are synchronized with local pictures, delete if not in local folder foreach (ResponseElement _element in _folderDesc.contents) { if (_element.is_dir) { _status &= await Delete(_element.path); } else { Boolean _found = false; foreach (Picture _picture in _album.Pictures) { if (_pathAlbum + "/" + _picture.Name + _picture.Extension == _element.path) { _found = true; } } if (!_found) { _status &= await Delete(_element.path); } } } if (!_status || CancelInProgress()) { return(false); } } //check if all local pictures are synchronized with dropbox pictures int _countParts = 0; foreach (Picture _picture in _album.Pictures) { Boolean _found = false; if (_folderDesc.contents != null) { foreach (ResponseElement _element in _folderDesc.contents) { if (_pathAlbum + "/" + _picture.Name + _picture.Extension == _element.path) { _found = true; } } } if (!_found) { UploadOperation _newUpload = await synchronizePicture(_picture, _pathAlbum, _countParts ++, _summary); if (_newUpload != null) { UploadList.Add(_newUpload); } } if (CancelInProgress()) { return(false); } } return(true); } return(false); }
public async Task <Boolean> SynchronizeTrip(Trip _trip) { Boolean _status = true; ResponseContainer _folderDesc = null; ProgressUpdate(Res.GetString("SynchroTrip") + " " + _trip.Summary.FolderTopName, 0); String _request = API_METADATA + "/sandbox/" + _trip.Summary.FolderTopName + "?access_token=" + Token + "&hash=" + _trip.Hash; HttpResponseMessage _response = await SendRequest(_request); if (_response != null) { if (_response.StatusCode == HttpStatusCode.OK) { _folderDesc = await Serialization.DeserializeHttpToJson <ResponseContainer>(_response.Content) as ResponseContainer; } else if (_response.StatusCode == HttpStatusCode.NotFound) { _folderDesc = await CreateFolder(_trip.Summary.FolderTopName); } else if (_response.StatusCode == HttpStatusCode.NotModified) { return(true); } } if (_folderDesc.contents != null) { //check if all dropbox folders are synchronized with local folders foreach (ResponseElement _element in _folderDesc.contents) { if (_element.is_dir) { Boolean _found = false; foreach (Album _album in _trip.Albums) { if ("/" + _trip.DisplayName + "/" + _album.PathAlbum == _element.path) { _status &= await synchronizeAlbum(_album, _trip.DisplayName, _trip.Summary); _found = true; } if (!_status) { return(false); } } if (!_found) { _status &= await Delete(_element.path); } } else { //not a folder, delete file _status &= await Delete(_element.path); } } } //check if all local folders are synchronized with dropbox folders foreach (Album _album in _trip.Albums) { Boolean _found = false; if (_folderDesc.contents != null) { foreach (ResponseElement _element in _folderDesc.contents) { if (_album.DisplayName == _element.path) { //already synchronized _found = true; } } } if (!_found) { _status &= await synchronizeAlbum(_album, _trip.DisplayName, _trip.Summary); } if (!_status || CancelInProgress()) { return(false); } } //get hash code of synchronized folder _request = API_METADATA + "/sandbox/" + _trip.Summary.FolderTopName + "?access_token=" + Token; _response = await SendRequest(_request); if ((_response != null) && (_response.StatusCode == HttpStatusCode.OK)) { _folderDesc = await Serialization.DeserializeHttpToJson <ResponseContainer>(_response.Content) as ResponseContainer; _trip.Hash = _folderDesc.hash; return(true); } //should never reach this code return(false); }