2

I have to write an application, no matter what language (c#, Java, shell, python ...) that can connect to OneDrive and then uploads file. Following the OneDrive API I found that i need in one step to go to the browser (manually and to post a url that combines client_id and client_security to get an authentication code so i can connect my client with it to get the access token. (oAuth2 protocol) I need to get the access_token pragmatically, i don't need any manual step to be involved. I tried in c# to use the WebBrowser component to navigate to the url and to get the access token, I found that the browser stays in the same url and not getting the final url that includes the auth_code! My code looks like:

 // Initialize a new Client (without an Access/Refresh tokens
            var client = new Client(options);

            // Get the OAuth Request Url
            var authRequestUrl = client.GetAuthorizationRequestUrl(new[] { Scope.Basic, Scope.Signin, Scope.SkyDrive, Scope.SkyDriveUpdate });

            // TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
            WebBrowser wb = new WebBrowser();
            wb.AllowNavigation = true;

            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(authRequestUrl);
            Console.WriteLine(wb.Version);
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
            wb.Document.InvokeScript("evt_Login_onload(event)");

            Uri myUrl = wb.Url;

Anyone can help with fixing this, or maybe suggest other ideas please?

Thanks in Advance!

Marwan Jaber
  • 611
  • 11
  • 27

4 Answers4

2

It looks like you're creating a Windows desktop app using C#. There's actually an example at https://msdn.microsoft.com/en-us/library/hh826529.aspx for using the WebBrowser class to get the authorization code, then the token, then make an API. In short, you'll first need to send a request to the following URL with your client_id and scopes.

https://login.live.com/oauth20_authorize.srf?client_id=YOUR_CLIENT_ID&scope=YOUR_SCOPE_STRING&response_type=code&redirect_uri=https://login.live.com/oauth20_desktop.srf

In the response, you'll get the authorization code which you'll need to use to send another request to with your client_id, client_secret, authorization code like the following.

https://login.live.com/oauth20_token.srf?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&redirect_uri=https://login.live.com/oauth20_desktop.srf&code=AUTHORIZATION_CODE&grant_type=authorization_code

When you finally receive the access token, you can make requests to the API using your access token similar to the following.

"https://apis.live.net/v5.0/me?access_token=ACCESS_TOKEN". The "me" can be changed to any other folder or directory.

I hope that helps.

Toan-Nguyen
  • 321
  • 1
  • 3
  • Thanks for your answer, Actually This example opens a browser with sign-in screen for the user to approve the request. I need to do it without asking the user for any requests, the idea that i have an automatic job that has to backup to my oneDrive all the folders every noghtly let's say and i need all the progress to be automated from A-Z – Marwan Jaber Mar 22 '15 at 09:09
  • 1
    You will initially have to receive consent from your users. After which, you can ask for a refresh token to complete additional requests later on. – Toan-Nguyen Mar 26 '15 at 23:26
0

dont u think the scope u provided are wrong, they should be wl.basic, wl.signin, and if ur using new onedrive api then it should be onedrive.readonly or onedrive.readwrite

if ur using liveconnect api for the purpose of using onedrive then scope should be wl.skydrive or wl.contacts_skydrive or wl.skydrive_update depending upon ur uses (refer https://msdn.microsoft.com/en-us/library/hh243646.aspx)

and can u more elaborate how ur trying to get the access_token, from above it is quite confusing to me

ankit
  • 1
  • 1
0

Have you solved you issue?

Have you tried to use the LiveSDK to authenticate?

Have a look at my question there, it might help you : Onedrive API vs LiveSDK

I have used the following code, after installing both the LiveSDK and the OneDrive SDK, and this does not require any login after the first authorization. However it "may" have to be a RT app (windows store or windows phone store)

var authClient = new LiveAuthClient();
var authResult = await authClient.LoginAsync(new string[] {  
        "wl.signin", "onedrive.readwrite", "onedrive.appfolder"});

if (authResult.Session == null)
    throw new InvalidOperationException("You need to sign in and give consent to the app.");

var Connection = new ODConnection("https://api.onedrive.com/v1.0", 
    new MicrosoftAccountAuthenticationInfo() { TokenType = "Bearer", 
    AccessToken = odArgs.Session.AccessToken });
Community
  • 1
  • 1
Jean
  • 4,911
  • 3
  • 29
  • 50
0

Toan-Nguyen's answer almost helps me. On the step 2 (when I should send a request with authorization code) I get the response with error "Public clients can't send client secret". This answer said it's neccessary to remove the attribute client_secret from url.

Community
  • 1
  • 1
Barabas
  • 912
  • 8
  • 19