OneDrive Client Authentication (SDK)

I play with OneDrive SDK 1.1.15.0:

try
{
    AppConfig appConfig = new AppConfig
    {
        MicrosoftAccountAppId = oneDriveClientID, //something like 00000000123456AB
        MicrosoftAccountClientSecret = oneDriveClientSecret, //something like 3vx[...]1sJ
        MicrosoftAccountReturnUrl = "https://localhost/return",
        MicrosoftAccountScopes = new string[] { "wl.signin", "wl.offline_access", "onedrive.readonly" }
    };
    OneDriveClient oneDriveClient = new OneDriveClient(appConfig);
    AccountSession accountSession = await oneDriveClient.AuthenticateAsync();

    //more code

    await oneDriveClient.SignOutAsync();
}
catch (Exception ex)
{
    throw ex;
}

My problem is in the line:

AccountSession accountSession = await oneDriveClient.AuthenticateAsync();

which raises the following exception:

Microsoft.OneDrive.Sdk.OneDriveException, AuthenticationFailure: Failed to retrieve a valid authentication token for the user.

Any ideas?

Thank you in advance!

UPDATE

After reading the comment from ginach (thanks!), I am updating my code. Some arguments emphasize:

  • I want to access OneDrive from the Azure working role, so there are no authentication windows or anything like that.
  • I am downloading the Microsoft.OneDrive SDK version in version 1.1.20.
  • I have already registered my application on the OneDrive dev portal.

My actual code is:

try
{
    MicrosoftAccountServiceInfo serviceInfo = new MicrosoftAccountServiceInfo();

    serviceInfo.AppId = oneDriveClientID; //something like: 00000000ABCDEFGH
    serviceInfo.ClientSecret = oneDriveClientSecret; //something like: 3vx[...]1sJ
    serviceInfo.ReturnUrl = oneDriveReturnUrl; //something like: https://localhost/return
    serviceInfo.Scopes = oneDriveAccountScopes; //something like new string[] { "wl.signin", "wl.offline_access", "onedrive.readonly" }

    MicrosoftAccountAuthenticationProvider authenticationProvider = new MicrosoftAccountAuthenticationProvider(serviceInfo);

    OneDriveClient oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(oneDriveClientID, oneDriveReturnUrl, oneDriveAccountScopes, authenticationProvider);

    //more code

    await oneDriveClient.SignOutAsync();
}
catch (OneDriveException odex)
{
    throw odex;
}
catch (Exception ex)
{
    throw ex;
}

I get again and again (in the OneDriveClient.GetAuthenticatedMicrosoftAccountClient method). OneDriveException (Error property): AuthenticationFailure - Failed to get a valid authentication token for the user.

Any suggestion?

Thanks.

UPDATE 2

, . RestSharp, OneDrive :

string clientId = "00[...]00";
string scopes = "wl.signin, wl.offline_access, onedrive.readonly";
string responseType = "code";
string redirectUri = "https://login.live.com/oauth20_desktop.srf";

RestClient client = new RestClient("https://login.live.com");
RestRequest request = new RestRequest();

request.Method = Method.GET;
request.Resource = "oauth20_authorize.srf";
request.AddQueryParameter("client_id", clientId);
request.AddQueryParameter("scope", scopes);
request.AddQueryParameter("response_type", responseType);
request.AddQueryParameter("redirect_uri", redirectUri);

IRestResponse response = client.Execute(request);
string content = response.Content;

Fiddler, , , :

https://login.live.com/oauth20_authorize.srf?client_id=00[...]00&scope=wl.signin%20wl.offline_access%20onedrive.readonly&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf

OneDrive :

Microsoft JavaScript. - JavaScript, . , JavaScript , .

, , OneDrive :

enter image description here

: - , ?

,

+4
1

, . .

  • IAuthenticationProvider. . , :

var client = new OneDriveClient (appConfig, serviceInfoProvider: ServiceInfoProvider ( CustomAuthenticationProvider()));

  1. . SDK .

  2. , OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient. :

var client = OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(clientId, returnUrl, scopes, refreshToken);

+1

Source: https://habr.com/ru/post/1620517/


All Articles