Step 1: Create and manage application on Live Connect Center
In first, you must sign-on the manager page of Live Connect Center, then create and manage your applications.
Fill out all of required information, it shows the Client ID for your client-side application.
Then now, we can start to write client-side application for using SkyDrive REST API.
Step 2: Implement client-side authentication
Difference with the guideline of user signing, I would like to use access token for client-side authentication, so there is a little change that I set the response_type=token .
Following is the request URL of Live Connect Oauth 2.0
https://login.live.com/oauth20_authorize.srf?client_id=CLIENT_ID&scope=SCOPE_STRING&response_type=token&redirect_uri=https://login.live.com/oauth20_desktop.srf
CLIENT_ID : the client id of managed application on Live Connect Center
SCOPE_STRING : permissions which gets from the user to access their info on app, see also guideline of scopes and permissions.
After user agree the consent, Oauth 2.0 API will return a URL that contains token and expired time, looks like
https://login.live.com/oauth20_desktop.srf?lc=1033#access_token=AsDfGhJkL1234&token_type=bearer&expires_in=3600&scope=wl.signin%20wl.skydrive
The access_token would be found in the URL fragment if user sign-in and authorized successfully.
Here is a sample code which I implement the authentication by using WebBrowser class in C#
private static string scope = "wl.signin%20wl.skydrive%20wl.skydrive_update";
private const string client_id = "0000000000012345";
private static Uri signInUrl = new Uri(String .Format(@"https://login.live.com/oauth20_authorize.srf?client_id={0}&scope={1}&response_type=token&redirect_uri=https://login.live.com/oauth20_desktop.srf", client_id, scope));
webBrowser.Navigate(signInUrl);
webBrowser.LoadCompleted += webBrowser_LoadCompleted;
private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (e.Uri.Fragment.Contains("access_token"))
{
if (App.Current.Properties.Contains("oauthData"))
App.Current.Properties.Clear();
App.Current.Properties.Add("oauthData", 1);
string[] responseOauth = Regex.Split(e.Uri.Fragment.Remove(0, 1), "&");
for (int i = 0; i < responseOauth.Count(); i++)
{
string[] nvPair = Regex.Split(responseOauth[i], "=");
App.Current.Properties.Add(nvPair[0], responseOauth[i]);
}
this.Close();
}
}
#file retrieving and uploading will be continued in the next article
沒有留言:
張貼留言