0
votes

I've been searching the net for a basic sample winforms app that is written in vb.net to upload a file to onedrive. Does anyone know of any?

I'm trying to get a file uploaded with a winforms app in vb.net. I'm as far as getting the auth working... but calling the next method returns a 401...

I did the following:

Shared scope As String = "wl.skydrive_update" Shared client_id As String = "0000000040144E26" Shared signInUrl As New Uri([String].Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&redirect_uri=https://login.live.com/oauth20_desktop.srf&response_type=code&scope={1}", client_id, scope))

Private Sub cmdOneDriveAuth_Click(sender As Object, e As EventArgs) Handles cmdOneDriveAuth.Click Try Dim auth As New FrmAuthBrowser auth.WebBrowser1.Navigate(signInUrl) auth.Show()

    Catch ex As Exception

    End Try
End Sub

and then in the auth window:

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Try
        If WebBrowser1.Url.AbsoluteUri.Contains("code=") Then
            Dim AuthCode As String = System.Web.HttpUtility.ParseQueryString(WebBrowser1.Url.Query)("code")
            My.Settings.OneDrive_Enabled = True
            My.Settings.OneDrive_AuthCode = AuthCode
            My.Settings.Save()
            Me.Dispose()
        End If


    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

but when I try and get the root info, I get a 401...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Dim client As New WebClient() Dim result = client.OpenRead(New Uri("https://apis.live.net/v5.0/me/skydrive?access_token=" + My.Settings.OneDrive_AuthCode)) Dim sr As StreamReader = New StreamReader(result) MsgBox(sr.ReadToEnd()) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub

Can anyone provide me with some guidance?

1

1 Answers

0
votes

Looks like you're using the 'code' flow of OAuth, but you're missing a step. The 'code' you get back isn't the same as the access_token. You need to make another call to the login server first to exchange your code for an access_token.

POST https://login.live.com/oauth20_token.srf
Content-Type: application/x-www-form-urlencoded

client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
&code={code}&grant_type=authorization_code

You can find more details here http://onedrive.github.io/auth/msa_oauth.htm#code-flow.

OneDrive just released a new API, so I would encourage you to check it out. http://onedrive.github.io/

There's also a sample Windows/C# app that you can look at for reference on how to sign in and upload files. https://github.com/OneDrive/onedrive-explorer-win