This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
DropboxViewModel.cs
141 lines (125 loc) · 4.18 KB
/
DropboxViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using Spring.Social.OAuth1;
using Spring.Social.Dropbox.Api;
namespace Spring.WindowsPhoneQuickStart.ViewModel
{
public class DropboxViewModel : INotifyPropertyChanged
{
private const string OAuthTokenKey = "OAuthToken";
public const string CallbackUrl = "http://localhost/Dropbox/Callback";
private OAuthToken requestOAuthToken;
private Uri authenticateUri;
private DropboxProfile profile;
public IOAuth1ServiceProvider<IDropbox> DropboxServiceProvider { get; set; }
public bool IsAuthenticated
{
get
{
return this.OAuthToken != null;
}
}
public OAuthToken OAuthToken
{
get
{
return this.LoadSetting<OAuthToken>(OAuthTokenKey, null);
}
set
{
this.SaveSetting(OAuthTokenKey, value);
NotifyPropertyChanged("IsAuthenticated");
}
}
public Uri AuthenticateUri
{
get
{
return this.authenticateUri;
}
set
{
this.authenticateUri = value;
NotifyPropertyChanged("AuthenticateUri");
}
}
public DropboxProfile Profile
{
get
{
return this.profile;
}
set
{
this.profile = value;
NotifyPropertyChanged("Profile");
}
}
public void Authenticate()
{
this.DropboxServiceProvider.OAuthOperations.FetchRequestTokenAsync(null, null,
r =>
{
this.requestOAuthToken = r.Response;
OAuth1Parameters parameters = new OAuth1Parameters();
parameters.CallbackUrl = CallbackUrl;
this.AuthenticateUri = new Uri(this.DropboxServiceProvider.OAuthOperations.BuildAuthenticateUrl(r.Response.Value, parameters));
});
}
public void AuthenticateCallback()
{
AuthorizedRequestToken authorizedRequestToken = new AuthorizedRequestToken(this.requestOAuthToken, null);
this.DropboxServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(authorizedRequestToken, null,
r =>
{
this.OAuthToken = r.Response;
this.ShowProfile();
});
}
public void ShowProfile()
{
IDropbox dropboxClient = this.DropboxServiceProvider.GetApi(this.OAuthToken.Value, this.OAuthToken.Secret);
dropboxClient.GetUserProfileAsync(
r =>
{
if (r.Error == null)
{
this.Profile = r.Response;
}
});
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private void SaveSetting(string key, object value)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains(key))
{
settings[key] = value;
}
else
{
settings.Add(key, value);
}
}
private T LoadSetting<T>(string key, T defaultValue)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains(key))
{
settings.Add(key, defaultValue);
}
return (T)settings[key];
}
}
}