Archive for the 'skydrive' Category

17
Mar
12

skydrive upload c# metro

Peter Daukintis

A preview of the latest Live SDK for Metro apps can be downloaded here https://connect.microsoft.com/site1226. This is for usage with the Windows 8 Consumer Preview (Note that the SDK is a preview version with no go-live license).

After installing you can add a reference via ‘add Reference’ > and select Windows > Extensions in the Reference Manager dialog.

livesdkRef

In order to configure sign in on Windows Phone a client ID was required; this is different for Metro-style Apps as you now need to register your app package name and publisher identity here http://go.microsoft.com/fwlink/?LinkId=227628.

Once registered you will be provided with a new Package Name – copy this back into your app manifest.

Once this is complete, using the following code you will be able to sign in:

XAML:

add this namespace declaration

xmlns:live="using:Microsoft.Live.Controls"

and a sign in button

AccessRequest

 

The Live id session status is reported by the SessionChanged event so I handle it like this, storing the session object for later use:

private void SignInButtonSessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        _liveConnectSession = e.Session;
        UpdateEnabled();
    }
}

Next we need a way to get hold of an image so I just used the File Picker to retrieve one (I added a way to capture an image using the camera but I couldn’t test it so it probably won’t work Sad smile)

Here’s the code for the file picker:

async private void ChooseFile(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    StorageFile file = await openPicker.PickSingleFileAsync();

    if (file != null)
    {
        _filename = file.Name;
        BitmapImage bitmapImage = new BitmapImage();
        _stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
        bitmapImage.SetSource(_stream);
        myImage.Source = bitmapImage;
        UpdateEnabled();
    }
}

and finally, what it was all about, uploading to skydrive:

private void Upload(Stream data, string filename)
{
    var liveConnectClient = new LiveConnectClient(_liveConnectSession);

    liveConnectClient.UploadCompleted += LiveConnectClientUploadCompleted;
    VisualStateManager.GoToState(this, "Busy", true);
    liveConnectClient.UploadAsync("me/skydrive", filename, true, data, data);
}

Here’s the ui whilst uploading an image:

uploadspam

 

and when completed – displaying the json result returned by the upload.

 

uploadresults

 

and finally, the image on skydrive.

 

spamonskydrive

The project can be found here https://skydrive.live.com/redir.aspx?cid=4f1b7368284539e5&resid=4F1B7368284539E5!441&parid=4F1B7368284539E5!123

18
Jan
12

wp7 skydrive upload

Technorati Tags:

Download the sdk, which at the time of writing is here http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=28195

Next, head over to https://manage.dev.live.com/Applications/Index and add an application. This will provide you with an application Id and a client secret token. You also need to configure a valid redirect url as required by the OAuth handshake.

 

liveappreg

 

Add a reference to Microsoft.Live and Microsoft.Live.Controls. (included with the SDK)

Add a SignInButton to your page.

 

<Controls:SignInButton x:Name="signInButton" Height="120"
                                   ClientId="XXXXXXXXXXXXXXXX"
                                   SessionChanged="SignInButtonSessionChanged"
                                   Scopes="wl.skydrive_update"
                                   RenderTransformOrigin="0.5,0.5" 
                                   d:IsHidden="True">

 

Now, you should be able to click on the sign in button, get redirected to the live login page.  After logging in you will be required to give consent for the requested authorisation. This will look like this:

 

authorise 

We then wire up a handler to the SignInButton SessionChanged event and if the sign in was successful we will get a LiveConnectSession object passed in the event.

            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                VisualStateManager.GoToState(this, "SignedIn", true);
                _liveConnectSession = e.Session;
            }

 

A LiveConnectClient object can be constructed from the session object and used to carry out standard operations, i.e. upload, delete, copy, etc.

 

Find the project here:

https://skydrive.live.com/redir.aspx?cid=4f1b7368284539e5&resid=4F1B7368284539E5!435&parid=4F1B7368284539E5!123




Follow

Get every new post delivered to your Inbox.