Beispiel #1
0
        private void SaveTokenToDisk(AuthenticationToken token)
        {
            FileStream fileStream = null;
            try
            {
                // If the file exists, decrypt it before changing its contents
                string filePath = Path.Combine(PathHelper.HomeDirectory, "dropbox.json");

                #if !MACOSX
                if (File.Exists(filePath))
                    File.Decrypt(filePath);
                #endif

                // Write file to disk
                string json = JsonConvert.SerializeObject(token);
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                fileStream = File.OpenWrite(filePath);
                fileStream.Write(bytes, 0, bytes.Length);
                fileStream.Close();

                #if !MACOSX
                // Encrypt file
                File.Encrypt(filePath);
                #endif
            }
            catch (Exception ex)
            {
                Tracing.Log("DropboxCoreService - SaveTokenToDisk - Failed to save token: {0}", ex);
                throw;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }
Beispiel #2
0
        private void ContinueLinkApp(AuthenticationToken token)
        {
            try
            {
                // Get access token either from parameter or from API
                OAuthToken oauthAccessToken = null;
                if (token == null)
                {
                    AuthorizedRequestToken requestToken = new AuthorizedRequestToken(_oauthToken, null);
                    oauthAccessToken = _dropboxServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(requestToken, null).Result;
                }
                else
                {
                    oauthAccessToken = new OAuthToken(token.Value, token.Secret);
                }

                if (OnCloudAuthenticationStatusChanged != null)
                    OnCloudAuthenticationStatusChanged(CloudAuthenticationStatusType.RequestAccessToken);

                // Get Dropbox API instance
                _dropbox = _dropboxServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret);
                //dropbox.Locale = CultureInfo.CurrentUICulture.IetfLanguageTag;

                // Test Dropbox API connection (get user name from profile)
                DropboxProfile profile = _dropbox.GetUserProfileAsync().Result;
                HasLinkedAccount = true;

                // Save token to hard disk
                SaveTokenToDisk(new AuthenticationToken() {
                    Value = oauthAccessToken.Value, 
                    Secret = oauthAccessToken.Secret,
                    UserName = profile.DisplayName
                });
                
                if (OnCloudAuthenticationStatusChanged != null)
                    OnCloudAuthenticationStatusChanged(CloudAuthenticationStatusType.ConnectedToDropbox);
            }
            catch (AggregateException ae)
            {
                HasLinkedAccount = false;
                ae.Handle(ex =>
                {
                    if (ex is DropboxApiException)
                    {
                        Console.WriteLine(ex.Message);
                        return true;
                    }
                    return false;
                });
            }
        }